javaranch.com for some reason isn't saving my intended post, so I'm trying here.
Am trying to set up a main menu to add contacts to a list:
public void showMenu() {//open method int mainChoice=0; Scanner mainMenuChoice = new Scanner(System.in); while (mainChoice!=4) {//open while loop System.out.println(); System.out.println("Main Menu:"); System.out.println(); System.out.println("1: Add Contact"); System.out.println("2: Edit/Delete Contact"); System.out.println("3: Search For Contact"); System.out.println("4: Quit Application"); System.out.println(); try { System.out.print("Enter Choice: "); mainChoice = mainMenuChoice.nextInt(); }catch (InputMismatchException e){ System.out.println("The entry must be a number between 1 and 4 inclusive"); showMenu(); } if (mainChoice==4){ break; } if (mainChoice==1) {//open if loop for choices addContact(); }//close if loop }//close while loop }//close showMenu() method
Here's the output problem:
Main Menu: 1: Add Contact 2: Edit/Delete Contact 3: Search For Contact 4: Quit Application Enter Choice: p The entry must be a number between 1 and 4 inclusive Main Menu: 1: Add Contact 2: Edit/Delete Contact 3: Search For Contact 4: Quit Application Enter Choice: 4 Main Menu: 1: Add Contact 2: Edit/Delete Contact 3: Search For Contact 4: Quit Application Enter Choice: The entry must be a number between 1 and 4 inclusive Main Menu: 1: Add Contact 2: Edit/Delete Contact 3: Search For Contact 4: Quit Application Enter Choice:
Not sure if I'm misunderstanding try/catch loops, variable scope, etc. the problem only occurs if I enter the number 4 after a non-integer entry. Can someone help me out?