Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: InputMismatchException -- I don't understand the logic here; missing something

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default InputMismatchException -- I don't understand the logic here; missing something

    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?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: InputMismatchException -- I don't understand the logic here; missing something

    Answered here: https://coderanch.com/t/750095/java/...atch-exception
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Getting InputMismatchException
    By Seth Lau.20 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2021, 07:21 AM
  2. How to fix InputMismatchException?
    By kukupie123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2019, 12:21 PM
  3. [SOLVED] InputMismatchException
    By mrivera85 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 4th, 2014, 11:49 AM
  4. [SOLVED] InputMisMatchException
    By jmack in forum Exceptions
    Replies: 3
    Last Post: January 26th, 2011, 10:30 AM
  5. INPUTMISMATCHEXCEPTION
    By james in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 15th, 2010, 01:02 AM