• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

cant get program loop to work?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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 ->

if(cont==JOptionPane.NO_OPTION);System.exit(0);

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
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
lowercase baba
Posts: 13013
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic