For one of my assignments we are supposed to try to catch a InputMismatchException and tell the user to input a given option (In this case it can be 1,2,3,4 and nothing else). For some reason, whenever I try to make the try catch block it makes the user enter input twice and it still crashes the program. Here is what I have:
do {
Scanner menuOptions = new Scanner(System.in);
System.out.println("1. Get another card");
System.out.println("2. Hold hand");
System.out.println("3. Print statistics");
System.out.println("4. Exit"+ "\n");
System.out.println("Choose an Option: ");
int selectedOption = menuOptions.nextInt();
try{
selectedOption = menuOptions.nextInt();
}
catch(InputMismatchException e){
System.out.println("Invalid input! Please select between options 1,2,3,4")
}
if (selectedOption == 1) {
//proceeds to do what is instructed if user input is 1}
else if (selectedOption ==2{
//proceeds to do what is instructed if user input is 2}
else if (selectedOption == 3) {
//proceeds to do what is instructed if user input is 3}
else if (selectedOption ==4{
//proceeds to do what is instructed if user input is 4}
Any idea on how I can make this work?