Everytime I run this code, the console goes into an infinite loop printing “Please input a number”. I do not understand why this is happening. Thanks in advance.
boolean check = true;
int temp = 0;
while(check==true){
try{
temp= asker.nextInt();
check = false;
}
catch(InputMismatchException e){
System.out.println("Please input a number.");
}
}
Edit: asker is a Scanner. The purpose of the code is to loop until an integer is inputted by the user.
The method
asker.NextInt()is throwing anInputMismatchException, indicating that the input received fromasker(assuming it’s aScanner) isn’t actually an integer. This exception causes the loop to restart without settingcheckto false.Print the exception within the catch block to get more information about the failure. But most likely, you’re feeding your application something (lots and lots of something, if it’s looping like that) that doesn’t actually contain integer values.