aspose file tools*
The moose likes Beginning Java and the fly likes looped menu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "looped menu" Watch "looped menu" New topic
Author

looped menu

paulio macmurray
Greenhorn

Joined: Oct 26, 2009
Posts: 9
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29275
    
140

Moving to Java In General since this isn't a JDBC question.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29275
    
140

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

Joined: Oct 26, 2009
Posts: 9
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

Joined: Sep 14, 2009
Posts: 1615
    
  10

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


When you do things right, people won't be sure you've done anything at all.
avijit majumder
Greenhorn

Joined: Sep 01, 2009
Posts: 15
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

Joined: Oct 26, 2009
Posts: 9
Hi all,

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

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 14, 2009
Posts: 1615
    
  10

You still have no way for the user to exit.... even though your syntax is off....

My suggestion?

Janus Engstrøm
Ranch Hand

Joined: Nov 01, 2006
Posts: 44
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: looped menu
 
Similar Threads
Reading a list of books and users from a file
non-static method cannot be referenced from a static context
reading input from a file into a array
Adding a new array element is not saving to memory
Scanner debugging help