How can I change the code to make the program skip an option after user input No?

69 Views Asked by At

I want the program to skip to "Enter another?" when N is entered after being prompted "Express?". How can I do that? Now, when N is entered, it still prompts "Long distance?"

Below is the current batch of code. Context: this is the data entry part of the code where user input gets added to an arraylist.

String input=null;
    
    //data entry (user input + adding to arraylist)
    do {
    
        System.out.println("Enter parcel code: ");
        String pCode=kboard.next();
        
        System.out.println("Enter parcel length: ");
        double pLength=kboard.nextDouble();
        
        System.out.println("Enter parcel width: ");
        double pWidth=kboard.nextDouble();
        
        System.out.println("Enter parcel height: ");
        double pHeight=kboard.nextDouble();
        
        System.out.println("Enter parcel weight: ");
        double pWeight=kboard.nextDouble();
        
        Parcel parcel=new Parcel(pCode, pLength, pWidth, pHeight, pWeight);
        
        List.add(parcel);
        
        System.out.println("Express parcel? Y/N: ");
        String expressP=kboard.nextLine();
        input=kboard.next();
        
        System.out.println("Long distance parcel? Y/N: ");
        String longDist=kboard.nextLine();
        input=kboard.next();
        
        System.out.println("Do you want to enter another parcel record? Y/N: ");
        input=kboard.next();
        
    }
    
    while (input.toLowerCase().equals("y"));
2

There are 2 best solutions below

1
On

Use 'if-else' conditioning after accepting the input for 'Express Parcel'

   System.out.println("Express parcel? Y/N: ");
   String expressP=kboard.nextLine();
   input=kboard.next();

   if(ip.equalsIgnoreCase("Y").  //This will execute only if previous resp is Yes
  { 
       System.out.println("Long distance parcel? Y/N: ");
       String longDist=kboard.nextLine();
      input=kboard.next();
  }

  System.out.println("Do you want to enter another parcel record? Y/N: ");
  input=kboard.next();

This way if user enter N or n it'll ask for next iteration skipping the code for Parcel distance.

3
On

Try the below code :

  String input=null;

    //data entry (user input + adding to arraylist)
    do {

        System.out.println("Enter parcel code: ");
        String pCode=kboard.next();

        System.out.println("Enter parcel length: ");
        double pLength=kboard.nextDouble();

        System.out.println("Enter parcel width: ");
        double pWidth=kboard.nextDouble();

        System.out.println("Enter parcel height: ");
        double pHeight=kboard.nextDouble();

        System.out.println("Enter parcel weight: ");
        double pWeight=kboard.nextDouble();

        /*Parcel parcel=new Parcel(pCode, pLength, pWidth, pHeight, pWeight);

        List.add(parcel);*/

        System.out.println("Express parcel? Y/N: ");
        String expressP=kboard.next();
        if (expressP.equalsIgnoreCase("Y")) {

            System.out.println("Long distance parcel? Y/N: ");
            String longDist = kboard.nextLine();
            input = kboard.next();
        }
        System.out.println("Do you want to enter another parcel record? Y/N: ");
        input=kboard.next();

    }

    while (input.toLowerCase().equals("y"));

you need to add an extra if to check the user has entered Y or not