0

My code below is looping twice. The 1st while loop before it asks for user input (to make a choice). I made it simpler by putting choice default is "N".

So it hits the if statement and begins the 2nd while loop. Now it asks the user for another choice. The user can only enter "A" as anything else will error trap. The user enters "A" and gets prompted to add a number (the variable num = 0). User enters a number.

The if statement closes, and the 2nd while loop comes back to the top, only it doesn't stop when the user is asked for a choice. Instead, it continues through the loop, hits the else statement, then comes back to the top of the 2nd while loop again and presents the user with a prompt for a choice.

Code Updated with More Information

while (true) {              // 1st while loop
choice="N";
   if (choice.equalsIgnoreCase("N")) {
       while (true) {                      // 2nd while loop
          System.out.println("|-|-| Add Number [A] Go Back [B]");
          System.out.println("NUMBER: "+num);
          System.out.print("Choice: ");
          choice = c.nextLine();

          if (choice.equalsIgnoreCase("A")){
             System.out.print("Add: ");
             num = (c.nextInt() + num);
             System.out.println("");
          }
          else if (choice.equalsIgnoreCase("B")){
             break;
          }
          else {
             System.out.println("ERROR 19: Invalid response");
             System.out.println("");
          }
       }
   }
}

I have tried using different variables for choice. It did not work. I think I may need to try catch just below the 2nd while loop (before the user is prompted for a number), but that's only a idea. My question would be, why is this happening? And if possible, how can I fix it?

user3735534
  • 1
  • 1
  • 2
  • 3
    You have two "forever" loops and no `break` or `return` statements, so how do you expect either loop to *ever* end? – Andreas Oct 21 '16 at 16:46
  • you shouldn't have your main loop while(true) and currently you have no breaks or anything to get you our of the loop so either add a break; when you are done or have a boolean variable set to true at the beginning and when you are done set the boolean variable to false – RAZ_Muh_Taz Oct 21 '16 at 16:46
  • 2
    _it doesn't stop when the user is asked for a choice_ This problem is related to usage of `nextInt` method. So this queston is a duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo). – Ravikumar Oct 21 '16 at 16:55
  • Andreas & RAZ, I updated my code. I do have a break statement when the user enters a B (to go back). Forgot to include it here. – user3735534 Oct 21 '16 at 17:23
  • @Ravikumar That worked! I posted my solution below. – user3735534 Oct 21 '16 at 19:26

4 Answers4

0

In your code while(true) will keep looping indefinitely. Either change the condition from always true to something which is conditionally true (using an if condition inside the while loop or a for loop). Or use a mix of break, return and continue when you think looping should stop.

Jaguar
  • 10,212
  • 20
  • 63
  • 114
  • Check my updated code. I originally left out the break part to make my code easier to understand. I added it now. – user3735534 Oct 21 '16 at 17:26
0

Add a break statement in both if and else statement :

         if (choice.equalsIgnoreCase("A")){
             System.out.print("Add: ");
             num = (c.nextInt() + num);
             System.out.println("");
             break;

          }
          else {
             System.out.println("ERROR 19: Invalid response");
             System.out.println("");
             break;
          }
Rehman
  • 3,465
  • 3
  • 17
  • 31
  • I included a break. That's not where I wanted it to be, as I want the user to be prompted again for a number (until they enter "B" to go back). Check my updated code. – user3735534 Oct 21 '16 at 17:24
0

I think you should change your loop. I suggest loop with like this :

boolean finished = false;
while(!finished) {
    ...
    choice = c.nextLine();
    if (choice.equalsIgnoreCase("A")){
        System.out.print("Add: ");
        num = (c.nextInt() + num);
        System.out.println("");
    } else if (choice.equalsIgnoreCase("Exit")) { //You can change this whatever you want
        finished = true;
    } else if (...){
       ... //another choice
    }
    else {
        System.out.println("ERROR 19: Invalid response");
        System.out.println("");
    }
}
0

The Scanner#nextLine consumes the full line (hence "next line") of the user input. That's why you never get a repeated loop while using nextLine. The Scanner#nextInt does not, and the last newline character is consumed the next time Scanner#nextInt is called.

To answer my question:

 if (choice.equalsIgnoreCase("A")){
         System.out.print("Add: ");
         num = (c.nextInt() + num); //Prompts user for input (number)
         c.nextLine();         // Consumes the last newline character
         System.out.println("");
      }
      else if (choice.equalsIgnoreCase("B")){
         break;
      }
      else {
         System.out.println("ERROR 19: Invalid response");
         System.out.println("");

 int option = input.nextInt();
 input.nextLine();  // Consume newline left-over
 String str1 = input.nextLine();

As answered from here: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Community
  • 1
  • 1
user3735534
  • 1
  • 1
  • 2