-2

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"));
  • You should use an if statement – Morph21 Apr 28 '22 at 05:44
  • thanks, I actually used that prior to making this post but it completely skipped over the long distance prompt. – diane shiki Apr 28 '22 at 05:56
  • you can use if statement inside do {} segment to skip anything you want, just make a correct statement for your use case. You have an example answer from @Shivam Papat so look there, change it to be working – Morph21 Apr 28 '22 at 05:58

2 Answers2

1

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.

Shivam Papat
  • 457
  • 4
  • 14
0

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

Prog_G
  • 1,539
  • 1
  • 8
  • 22
  • hey thanks! however it skipped the long distance prompt when Y is entered – diane shiki Apr 28 '22 at 06:01
  • @dianeshiki maybe you are entering Y in a lowercase. I have edited by answer to handle case in the code just change `equals` to `equalsIgnoreCase` in the if statement – Prog_G Apr 28 '22 at 06:06
  • @dianeshiki I have edited the answer try the answer again – Prog_G Apr 28 '22 at 06:12