#Why is this code an infinite while loop?
1 messages · Page 1 of 1 (latest)
private static int integerInputManager()
{
int returnValue = -1;
while(returnValue<1 || returnValue>50)
{
try {
System.out.println("Please enter a value between 1 and 50.");
returnValue = sc.nextInt();
if(returnValue<1 || returnValue>50)
{
System.out.println("Your entry is out of range.");
}
}catch (InputMismatchException e){
System.out.println("Please enter a correct value");
}
}
return returnValue;
}
Detected code, here are some useful tools:
System out
[Nothing]
from how i interpret it it should go back and give input
but it just skips over the input
this is if i input non numerical symbols
number input works
If they don't enter a number (Or the buffer doesn't currently contain a number), then it will just constantly get InputMismatchException without waiting for use input.
You should use .nextLine() for line-based input (from a user) and then parse/validate the result.
eg Integer.parseInt(sc.nextLine()) and catch the NumberFormatException.