Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

looped menu  RSS feed

 
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

 
author & internet detective
Posts: 39286
743
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
Posts: 39286
743
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
 
Bartender
Posts: 1840
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
 
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.

 
Author
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
Bartender
Posts: 1840
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?

 
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
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!