Results 1 to 2 of 2
  1. #1
    ndsun is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default using a do-while loop to process a menu selection

    I am a beginner in java ,when i run code below,
    Java Code:
    //using a do-while to process a menu selection
    class Menu
    {
    public static void main(String args[])
    throws java.io.IOException
    {
    char choice;
    do
    {
    System.out.println("Help on: ");
    System.out.println("1.if ");
    System.out.println("2.switch ");
    System.out.println("3.while ");
    System.out.println("4.do-while ");
    System.out.println("5.for\n ");
    System.out.println("Choose one: ");
    choice=(char) System.in.read();
    }while(choice <'1'||choice>'5');
    System.out.println("\n");
    switch(choice)
    {
    case '1':
    System.out.println("The if:\n ");
    System.out.println("if(condition) statement;");
    System.out.println("else statement;");
    break;
    case '2':
    System.out.println("The switch:\n ");
    System.out.println("switch(expression) { ");
    System.out.println("case constant:");
    System.out.println("statement sequence");
    System.out.println("break;");
    System.out.println("//...");
    System.out.println("}");
    break;
    case '3':
    System.out.println("The while:\n");
    System.out.println("while(condition) statement;");
    break;
    case '4':
    System.out.println("The do-while:\n");
    System.out.println("do {");
    System.out.println(" statement;");
    System.out.println("while(condition);");
    break;
    case '5':
    System.out.println("The for:\n");
    System.out.println("for(init;condition;iteration)" );
    System.out.println(" statement;");
    break;
    }
    }
    }
    when I run above code with the help of command prompt,it appears
    C:\Java\jdk1.7.0\bin>javac Menu.java

    C:\Java\jdk1.7.0\bin>java Menu
    Help on:
    1.if
    2.switch
    3.while
    4.do-while
    5.for

    Choose one:
    9
    Help on:
    1.if
    2.switch
    3.while
    4.do-while
    5.for

    Choose one:
    Help on:
    1.if
    2.switch
    3.while
    4.do-while
    5.for

    Choose one:
    Help on:
    1.if
    2.switch
    3.while
    4.do-while
    5.for
    Please tell me why after entering 9, 3 times such help menu is appearing ,while it should appear only one time.(it is happening with all values which're <1 and >5).
    Last edited by Fubarable; 07-02-2012 at 04:45 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: using a do-while loop to process a menu selection

    Don't call System.in.read() directly. I'm sure that this isn't how your instructor told you to do this or how your example programs show you to do this. You're better off using a Scanner object initialized to System.in.

Similar Threads

  1. Replies: 0
    Last Post: 09-21-2009, 11:33 AM
  2. Replies: 0
    Last Post: 07-07-2008, 04:47 PM
  3. How to use SWT menu and menu event
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:46 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

Tags for this Thread

Posting Permissions

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