aspose file tools*
The moose likes Beginning Java and the fly likes Looping program problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Looping program problems" Watch "Looping program problems" New topic
Author

Looping program problems

Brendan Cregan
Ranch Hand

Joined: Nov 11, 2011
Posts: 35
Hi All,

Im working on a console based Library management system in java, my interface is done through the use of a console like this:


The problem is that after i create a book i want to go back to the main menu but it just keeps exiting!im sure its a very easy fix but anyhelp would be much appreciated!!!

Thanks

Brendan
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Brendan Cregan wrote:
The problem is that after i create a book i want to go back to the main menu but it just keeps exiting!im sure its a very easy fix but anyhelp would be much appreciated!!!


Your thread subject says you already know the answer: Looping.

So is the problem that you don't know how to loop in Java? If so, then a quick google search will turn up tons of examples. If that's not the problem, you'll have to provide more details about what in particular is giving you trouble.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Also, extremely long lines like this one:



make your code hard to read and put forum controls so far to the right that I have to scroll to get to them.

There's nothing gained by doing that. Put read con.readLine() call on its own line, and assign the result to a variable. Your code will easier to read, understand, and debug.
Brendan Cregan
Ranch Hand

Joined: Nov 11, 2011
Posts: 35
Hi, thanks for so many response's so quickly!
i know how to loop but as the problem is that when i create an item e.g book the program exits, and was just wondering how to get it to start from the main menu again?
maybe my implementation of a console interface is off?

so just to clarify the problem, at the moment the program starts:
i type 1 then im brought into create item menu
then i type 1 again and the console asks for details of a book
then program exits!but instead of exiting i want it to return to the start of the program.

Another side question for storing this information would i be best off to use a hashtable or hashmap?

Thanks again

Brendan

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Brendan Cregan wrote:Hi, thanks for so many response's so quickly!
i know how to loop but as the problem is that when i create an item e.g book the program exits


Because you're not looping.

and was just wondering how to get it to start from the main menu again?


By putting the stuff that you want to repeat into a loop. I wonder if perhaps you're making the common mistake of thinking that "if" is a looping structure. It's not. It just makes a choice. Once. A loop is something that repeats.

Another side question for storing this information would i be best off to use a hashtable or hashmap?


Either one will work fine for you, but go with HashMap. It was created as part of the Collections Framework in Java 1.2, whereas Hashtable was retrofitted into it and still has old-style redundant methods.
Brendan Cregan
Ranch Hand

Joined: Nov 11, 2011
Posts: 35
Thanks for the response, but it doesn't really help me, "Because you're not looping" isn't exactly helpful, i clearly don't understand if you can see the solution.
i don't think its an issue with looping but rather my approach to the problem because if i continue the way im going i will have 100's of if statements for only 4 pages of a menu...attempted using jFrames but i think its overkill.
so still no solution, thanks for atleast looking Jeff.
Thanks

Brendan
Brendan Cregan
Ranch Hand

Joined: Nov 11, 2011
Posts: 35
managed to solve this by putting in while(i==1) and creating i=1.not sure if its good practice but it works!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Brendan Cregan wrote:Thanks for the response, but it doesn't really help me, "Because you're not looping" isn't exactly helpful


You said you know how to loop, but yet you're not doing it. Did you google for a java looping tutorial like I suggested?

, i clearly don't understand if you can see the solution.


I know exactly how to solve your problem.

i don't think its an issue with looping


Yes, it is. You want to repeat. That requires a loop. You have no loop. Hence, no repeating, because of your problem with (i.e., total lack of) looping.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Brendan Cregan wrote:managed to solve this by putting in while(i==1) and creating i=1.not sure if its good practice but it works!




would be the common idiom to loop forever.

Or, if you want to loop until your user gives you some "done" indication, you might do something like:



There are many slight variations on this, but the basic idea is the same: Keep going until some condition is met, expressed by "while (that condition is not yet met)".
Brendan Cregan
Ranch Hand

Joined: Nov 11, 2011
Posts: 35
thanks Jeff,
that really helps!now on with the next task of storing the data!think I will opt for hashmap!
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Looping program problems
 
Similar Threads
Do not know how to code some cases and exit the loop with "Enter"
Storing elements in array list or hash map
Error while trying to add an object to array list
Search arraylist of objects
[HOMEWORK] Please need help with java project