int choice = 0;
while ((choice != 1) || (choice !=2)){
System.out.println("1. Option 1");
System.out.println("2. Option 2");
try{
choice = scanner.nextInt();
}
catch(InputMismatchException e){
System.out.println(e);
}
if (choice == 1){
System.out.println("Option 1");
break;
}
if (choice == 2){
System.out.println("Option 2");
break;
}
System.out.println("Please enter a valid choice.");
}
I am trying to handle the case where a non int is entered. Why does this spin into an infinite loop? It worked fine before I tried to add in the try catch block.