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 6 of 6

Thread: Trying to create a loop for the user to press enter to return to main menu

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Trying to create a loop for the user to press enter to return to main menu

    I am having trouble with returning to the main menu.

    public static void main(String[] args)
    {
    Project4 app = new Project4();

    //Display all Menu options
    app.displayMenus();
    //Asks user to pick a menu option
    System.out.println("Please choose a menu option:");
    int option = keyboard.nextInt();
    while(option >=1 || option <= 5)
    {
    if(option == 1)
    {
    app.menu1();
    app.displayMenus();
    System.out.println("Please choose a menu option:");
    option = keyboard.nextInt();
    }

    if(option == 2)
    {
    app.menu2();
    app.displayMenus();
    System.out.println("Please choose a menu option:");
    option = keyboard.nextInt();
    }
    else if(option == 3)
    {

    app.menu3();
    System.out.println("Press enter to return to the menu.");
    String enter = keyboard.nextLine();

    }

    So the statement else if(option == 3) is supposed to display my menu3() method which displays information to the user. I am trying to get it to press enter to return to the main menu which is called with the method app.displayMenus(). I have tried the loops, but I just don't know what to put after String enter = keyboard.nextLine() to get it to the main menu and start the process over again. Any help would be appreciated. Thanks!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,253
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Trying to create a loop for the user to press enter to return to main menu

    Sounds like a job for a while loop?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Trying to create a loop for the user to press enter to return to main menu

    I don't know how to write it though because the else if statement before it confuses me on what to put in the while loops boolean expression
    else if(option == 3)
    {
    while(?)
    {
    app.menu3()
    System.out.println("Press enter to return to the main menu");
    String enter = keyboard.nextLine();
    }

    then do I put the main menu method after it or what?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,253
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Trying to create a loop for the user to press enter to return to main menu

    What exactly do you want to happen when the user presses enter after pressing 3?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Trying to create a loop for the user to press enter to return to main menu

    when they press enter it should return them to the main menu to press a number again. The main menu method is app.displayMenus();

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,253
    My Mood
    Hungover
    Thanks
    142
    Thanked 617 Times in 528 Posts

    Default Re: Trying to create a loop for the user to press enter to return to main menu

    It still seems like a while or a do while is your best option. Just save what the user enters and start over (maybe use the continue statement).

    If you want more help, you'll have to provide an SSCCE that we can actually run- just take out the secondary menu methods, but have the program flow be the same with a clearly stated goal of how you want to change it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 5
    Last Post: July 7th, 2011, 09:22 AM
  2. Replies: 21
    Last Post: May 29th, 2011, 12:48 PM
  3. Help - Return to menu screen
    By throzen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2009, 12:44 PM
  4. Replies: 1
    Last Post: October 23rd, 2009, 03:17 PM
  5. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 05:08 AM