-1

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();
      }
    }
Munesawagi
  • 283
  • 1
  • 6
  • 14

1 Answers1

0

You need to get rid of the userInput.next() in the catch clause.

catch (NumberFormatException e) {
    //userInput.next();
}

Just let it continue the loop if exception occurs.

Nick
  • 114
  • 5