I am working on a project (a game) and one of the requirements is to have warnings if they enter an option outside of the range (1-8) and if they enter a character. If the user enters an invalid number, the menu appears and asks them again which option they would like. If they enter a character, the program should ask them to enter an integer and ask for input again. This is what I have so far. It correctly identifies a number out of range and recalls the menu. It also identifies a character (invalid input), but does open input for the user to enter a correct option. How can I check both conditions?
Thanks, Tyler
int userChoice = scnr.nextInt();//<-- use this variable
if (userChoice.hasNextInt() == false)
{
System.out.println("Error: Menu selection must be an integer! Please try again:");
}
// Variable used for storing what the user's main menu choice
if (0 > userChoice || userChoice > 8)
{
System.out.println("Error: Invalid Menu Selection.");
System.out.println("");
System.out.println("Available Actions:");
System.out.println("(1) Print Market Prices");
System.out.println("(2) Print Detailed Statistics");
System.out.println("(3) Buy Some Sheep");
System.out.println("(4) Buy a Guard Dog");
System.out.println("(5) Sell a Sheep");
System.out.println("(6) Sell a Guard Dog");
System.out.println("(7) Enter Night Phase");
System.out.println("(8) Quit");
System.out.println("What would you like to do?");
userChoice = scnr.nextInt();
}