0

So far in my code I prompt the user to enter a positive integer representing the number of people they are inviting to an event. I already have an if statement to return an error message if the user input is a negative value. But how do I return an error message if the user enters a character, string, or double? Whenever I test this by entering a letter, the terminal just displays the following message:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Cookies.main(Cookies.java:15)
Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Clarisa
  • 125
  • 2
  • 12

1 Answers1

0

Catch the exception then display the message such as

try
{
int i = Integer.parseInt(input);
}
catch(NumberFormatException e)
{
System.out.println("You didn't enter an integer");
}
Joe
  • 337
  • 2
  • 16