Results 1 to 2 of 2
  1. #1
    Johnjohn is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default InputMismatch Exception inside do while menu, please help!

    Hello people, I'm newbie to java and wants to know where did I got wrong in my code to print the do while menu, the option available is from 1-9 and it's int, when i input string it catch the InputMismatch exception, my code has caught it but here got some problems.

    boolean menu = false;
    int option = 0;
    do{
    System.out.println("\t \t MENU \n \n");
    System.out.println("\t 1. aaa \n");
    System.out.println("\t 2. bbb \n");
    System.out.println("\t 3. ccc \n");
    System.out.println("\t 4. ddd \n");
    System.out.println("\t 5. eee \n");
    System.out.println("\t 6. fff \n");
    System.out.println("\t 7. ggg \n");
    System.out.println("\t 8. hhh \n");
    System.out.println("\t 9. iii \n");
    while (!menu){
    System.out.print("\t Enter Your Option : ");
    try{
    option = console.nextInt();
    menu = true;
    }

    catch ( InputMismatchException ex )
    {
    System.out.println();
    System.out.println("\t Wrong input, option valid is from 1-9" );
    System.out.println();
    String flush = console.next();
    }
    }
    if (option == 1){
    System.out.print("\t Enter Name : ");
    String name = console.next(".*");
    System.out.println("");
    System.out.println("Name : "+name);
    }

    .
    .
    .
    .
    .
    .
    .
    else if (option == 9){
    System.out.print("\t Byebye ");
    break;
    }
    }
    while(option != 9);




    When I input option (String) it can catch the exception and ask for option again, but when I input option 1, and write the name (String) it print the menu again, but then it ask for name input, not the option input, how to solve the problem??thank you so much for your guide :D

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    it catch the InputMismatch exception
    please copy and paste here the full error message.
    If you can include everything shown on the console so we can see what was entered.

    Add a call to printStackTrace() to all the catch blocks in your code.

    When you post some code please surround the code in code tags. See: http://www.java-forums.org/misc.php?do=bbcode#code

    Also a full program that compiles and executes makes it easier for anyone to test it.
    Last edited by Norm; 06-05-2011 at 08:35 PM.

Similar Threads

  1. Menu Type cast Exception
    By arulmozs in forum AWT / Swing
    Replies: 8
    Last Post: 11-02-2009, 10:34 PM
  2. no menu, no exception
    By cdene in forum New To Java
    Replies: 0
    Last Post: 10-02-2009, 01:50 AM
  3. Fill a menu dynamically when menu is shown
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  4. React to menu action and checkbox menu
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:50 PM
  5. Replies: 7
    Last Post: 05-09-2008, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •