jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes cant get program loop to work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cant get program loop to work? " Watch "cant get program loop to work? " New topic

cant get program loop to work?

David Triple

Joined: Jul 05, 2011
Posts: 20
I have been going at this for several hours for the last 4-5 days and cant seem to figure out where i am going wrong.
I have a dialog box to ask the user if they want to enter a loan. IF yes is pushed it goes one with the prgram, if no is pushed it still goes on without exiting, and
once the end of the program is reached it wont loop back to the dialog box asking to enter a loan.. Can any one spot my error? this is driving me crazy!

John Jai

Joined: May 31, 2011
Posts: 1776
Before we go to the fixes, I advise you to learn the constructs or syntax of Java first.
1. while loop will execute statements that are placed inside its opening & closing braces. There are no statements inside your while to tell which statements you need to execute till the condition matches. Hence the code should be like

2. The program checks for the "No" condition only at the last after executing all the logical statements to Principle, Interest, etc. So to terminate the program once after user entering "No" option have your code like below

3. Ok now you see this code you wrote ->
There is a ; after your if condition meaning that the if statement ends by then and the System.exit(0) will always be executed even if the option is yes / no (to be precise - when the execution reaches that point it will be executed for sure). So if your original logic was that program should be terminated if user gave No option then provide the System.exit(0) inside the braces of the condition.
4. Ok... at last please have your code nicely indented without unnecessary white spaces for your easy understanding.

And of course welcome to the Ranch
David Triple

Joined: Jul 05, 2011
Posts: 20
AHHH! Thank you so much, i was at the point of nervous break down trying to figure out how the syntax on loops work. I can do
i<10 loops all day long, but when you use input form GUI boxes it made things a lot more complicated!! Now to clean up my code for readability!!
This has got to be the best java forum on the net! no doubt!
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

Your code is very hard to read, since it does not follow the java coding convention. So, I may not be reading your code correctly...

I see a for-loop defined on lines 94-114. Then, there appears to be another loop - this time a while-loop, on line 116.

This second loop doesn't look like it does much, although it may have the potential to loop forever.

Is that what you intended - or is this all supposed to be one single loop?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John Jai

Joined: May 31, 2011
Posts: 1776
You are welcome!
I agree. Here's the link: http://aspose.com/file-tools
subject: cant get program loop to work?
It's not a secret anymore!