User input gets stuck in a loop

python while loop user input
python input validation loop
how to take continuous input in python
how to respond to user input in python
infinite loop in python
how to store user input in python
python user input
how to loop questions in python

I just started learning java today and decided to make a multiple choice quiz. The problem I've run into is that the user input gets stuck in a loop even if its one of the correct choices "a" or "b". This i my code:

    Scanner input = new Scanner(;

    String answer = input.nextLine();

    while(!answer.equals("a") || !answer.equals("b")) {
        System.out.println("Please enter one of the correct choices");
        answer = input.nextLine();

Can anyone let me know what I'm doing wrong here?


The error is in the usage of the OR operator ("||") in your while loop conditions. While these conditions are true, your loop will keep executing. Since you are checking if the user input is different than "a" or different than "b", the condition will always be true, because the input can't be "a" and "b" at the same time.

You should use the AND operator ("&&") in this case. See below:

while(!answer.equals("a") && !answer.equals("b")) {

This will return true (and thus keep executing the loop) if the input is, at the same time, different than "a" and different than "b".


Alerra also proposed a very interesting way to achieve the same as shown above. See below:

while(!(answer.equals("a") || !answer.equals("b"))) {

Mastering VBA for Microsoft Office 2007, You may want to temporarily disable user input while a procedure is LOOP Never disable user input for any code that may get stuck in an endless loop. I just started learning java today and decided to make a multiple choice quiz. The problem I've run into is that the user input gets stuck in a loop even if its one of the correct choices "a" or "b".

The issue is with the || operator, if answer is A than it is not B inversely if it is B than it is not A

How to Indefinitely Request User Input Until Valid in Python, Let's get to work! Create an infinite loop. The first step is to create an infinite loop. Normally, when you hear infinite loop, it's in the context of  You could declare an empty array before the loop, and then within the loop you could get an input from the user and append it to the array. after the loop had completed you would have an array containing all the user inputs in order and they could be accessed via their indexes

while(!(answer.equals("a") || !answer.equals("b")))

You can use truth table of OR to think of this question:

True True = True
True False = True
False True = True
False False = False

So if the user are not answering A and B, you want to keep the while loop running, inverse the False, so that the loop continue. If user answer any of A and B, it will break the loop!

Inverse OR truth table:

True True = False
True False = False
False True = False
False False = True

Software Engineering from Scratch: A Comprehensive Introduction , the program receives the user input and prints out the corresponding output, the If that defined condition is never met, you may be trapped in an infinite loop​  When you get into programming loops in the C language, you discover the joys and dreads of endless, or infinite, loops. These loops continue forever because either the programmer forgot to include a way to exit from the loop or the exit condition is just never met. Either way, endless loops are a pain. But …

While Loops and Input, They set up an infinite loop that runs until the user does something to end the loop. This section also introduces the first way to get input from your program's users. This is an infinite loop that is exited when the user enters -99. Any positive integer response will prompt the code to say Got "some number" followed by how many valid numbers read so far, whereas a negative integer, zero, or a non-numeric input will give a diagnostic message on standard error.

Infinite loop, Modern interactive computers require that the computer constantly be monitoring for user input or device activity, so at some fundamental level there is an infinite  That's because the sc.nextInt doesn't actually get the f off the input stream, it just throws the exception leaving the f still to be read by the next call to You could try using to read in the number into a String and then use Integer.parseInt to convert the String to a number.

What to Do if Your Program Infinite Loops, Unfortunately, a program waiting for input ellicits the same behavior. First, let me talk about the situation where your program is infinite looping. Once the candy cane is spinning again, select the project window, then use the View menu to  I need the user to input the screw size and whether or not to continue (until there are no more screws to sort). I cannot get the while loop to work correctly. I would like to ask the user "Continue? y/n: " and if the user types y, then the loop would continue, and if n, then the loop would stop and display my output.

  • Your while loop always evaluates to true so it will loop forever. Right now the the expression of the while loop will evaluate to true if the input is not "a" or not "b". I think you want not ("a" or "b"). I put parenthesis there as a hint.
  • is this what you mean: answer != (a || b)
  • Sort of, but the exact syntax would be !(answer.equals("a") || answer.equals("b")), which, by DeMorgan's Laws, is equivalent to what Talendar's answer is. So, you were on the right track.
  • Ah I see! Thanks so much for your help
  • You are welcome! If this answered your question, please, select this as the answer so it can help others in the future.
  • How do I do so? Sorry I'm new to stackoverflow
  • There is a grayed out "tick" icon on the left side of my answer (close to the "up" and "down" arrows). Just click it and this answer will be accepted by you.