• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

looped menu

 
paulio macmurray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following code works, but after an action is complete the program ends, but i dont want it to do that, i want the user to be returned to the menu.

Im fairly new with it, and im pretty sure i need a while loop, but i have no idea how to impliment it.

Here is my code so far

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34207
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java In General since this isn't a JDBC question.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34207
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paulio,
A "do-while" loop is traditionally used for this purpose. Can you identify which parts of the code you want to run again? That's the body of the loop. And how do you know you are done? (you probably want another option for "quit" so this doesn't go on forever)
 
paulio macmurray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanks for the response.

The pseudo code i wrote was.

1. user selects menu item (eg add book) (which i have)
2. the user inputs data requested via scanner class (which i have)
3. the data is displayed and user asked if they wish to submit.(which i have)
4. If yes, the data is stored (which i have) and the user is returned to the menu (which im stuck with)
5. If no, the user is returned to the menu (which im stuck with)

I agree that i need an exit button, which i have since found, but my brains given up on me
 
Janeice DelVecchio
Saloon Keeper
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're going to need a Do/While statement like another rancher said.... it'll look like this:



so the do before the while causes the loop to be done at least once. The user choice will let the JVM know if the user wants to exit. You could also ask the user if s/he'd like to keep going and reverse the logic if you like it that way....

Hope that helps
 
avijit majumder
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int choice=0;
while(true)
{
System.out.println("\n press 1 for ... , 2 for .... 3for .... , 4 for EXIT");
choice=Integer.parseInt(br.readLine());
switch (choice)
{
case 1://do somthing...call a method...break;
case 2://..... berak;
case 3://...do some thing break;
case 3: system.exit(1);// or break;
defaule: System.out.println("\n Wrong Key Press TRy Again!");
}
}//end while

 
paulio macmurray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have the following so far, but could do with some help to get the syntax correct.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you look at a Java tutorial or language spec, what does it say about the while syntax? It's probably something pretty close to if-statement syntax.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still have no way for the user to exit.... even though your syntax is off....

My suggestion?

 
Janus Engstrøm
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding your syntax, it's a good idea to end what you've started before beginning something new:

First:



Then add the stuff in the belly of your do-while loop...



(or use an IDE that handles this bracketing for you...)


Kind regards,
Janus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic