• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Looping program problems

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jeff,
that really helps!now on with the next task of storing the data!think I will opt for hashmap!
Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic