I am trying to use Integer.parseInt()
to convert a String
into an int
, and if that doesn't work, catch
the NumberFormatException
that the program returns and reprompt the user.
However, when I enter a non-String
value, instead of reprompting me, the program freezes.
Here is my code snippet:
keepLooping = true;
while(keepLooping) {
String unconvertedString = "";
int convertedInt = 0;
try {
System.out.print("Enter a string to be parsed into an integer: ");
unconvertedString = userInput.next();
convertedInt = Integer.parseInt(unconvertedString);
keepLooping = false;
}
catch (NumberFormatException e) {
userInput.next();
}
}