Question

int option=0;

while (option1!=1 || option1!=2){                   
    System.out.println("Give 1 for the first list which includes what we have in our exhibition");
    System.out.print("and 2 which we have not:"); // 2 print because i want to show at two different lines
    option1= Integer.parseInt(in.nextLine());} // when i give 1 or 2 as an option it doesn't goes out frome the loop
Was it helpful?

Solution

This is an infinite loop:

while(option1!=1 || option1!=2)

Since option1 can never simultaneously equal both 1 and 2, then this condition will always evaluate to true and the loop will always continue. You probably meant to use the logical "and" operator (&&) in your comparison:

while(option1!=1 && option1!=2)

That way the loop will end if option1 ever equals one of those two options.

OTHER TIPS

You need to say option1!=1 && option1!=2, not option1!=1 || option1!=2

Please remember to format your code so it's readable. Just add four spaces in front of each line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top