• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

looped menu

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java In General since this isn't a JDBC question.
 
Jeanne Boyarsky
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic