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

Thread: Menu Problem

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Menu Problem

    Ahoy. I'm trying to write me a menu driven program, and I'm having a heck of a time figuring out how to break out of a loop and up a level.

    Scanner stuff here, user input variable from keyboard object,etc.
    boolean menu = true;
    if (!menu)

    (input == 1)
    Display "You picked menu 1. You can now pick a cat or dog."
    Get input here.
    if (choice == dog)
    Do X
    else if (choice == cat)
    Do Y
    else
    Go back to the first menu.

    else if (input == 2)
    Do something else.


    How do I get it to break out of the nest if statement and "re run" the first menu? I'm trying to build a list of nested menus and it's driving me nuts.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Menu Problem

    How about a do-while loop? The while can evaluate a condition for exiting the loop

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Menu Problem

    How do I get the external loop to start over tho?

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Menu Problem

    What do you mean? The while loop will repeat until it evaluates its expression to false or break is called.

  5. The Following User Says Thank You to copeg For This Useful Post:

    StuckInJava (March 23rd, 2012)

  6. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Menu Problem

    I think I got it. Here's the code for anyone who needs some help with this in the future:
    		boolean run = false;
    		boolean run2 = false;
    		do
    		{
    			System.out.println("Introduction.");
    			System.out.println("Menu 1 ( 1, 2, or 3)");
    			int menu1 = input.nextInt();
     
    			if (menu1 == 1) 
    			{
     
    				do	{
    					System.out.println("Menu Level 2, option 1 - Select next level: ( 4, 5, 6 (Go back)");
    					int menu2 = input.nextInt();
    					if (menu2 == 4)
    					{
    						System.out.println("Menu 2, option four.");
     
    					}
    					else if (menu2 == 5)
    					{
    						System.out.println("Menu 2, option 5");
     
    					}
    					else if (menu2 == 6)
    					{
    						System.out.println("Breaking out of menu level 2, going back to menu level 1");
    						break;
    					}
     
    				}
    				while (!run2);
    			}
     
     
    			else if (menu1 == 2)
    			{
     
    				do	{
    					System.out.println("Menu Level 2, option 1 - Select next level: ( 4, 5, 6 (Go back)");
    					int menu2 = input.nextInt();
    					if (menu2 == 4)
    					{
    						System.out.println("Menu 2, option four.");
    					}
    					else if (menu2 == 5)
    					{
    						System.out.println("Menu 2, option 5");
    					}
    					else if (menu2 == 6)
    					{
    						System.out.println("Breaking out of menu level 2, going back to menu level 1");
    						break;
    					}
     
    				}
    				while (!run2);
    			}
     
    		} while (!run);
    		}
    }

Similar Threads

  1. Switch problem. How do I return to my menu?
    By jonny007 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2012, 11:54 AM
  2. Problem with actionlistener/drop-down-menu (for a queue)
    By blueroselara in forum AWT / Java Swing
    Replies: 17
    Last Post: January 25th, 2012, 10:59 PM
  3. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 08:19 AM
  4. problem with help menu.
    By Toggo in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 12th, 2010, 04:13 PM
  5. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM