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?