• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

while loop stuck  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so should it be a do while loop or just the while loop

while (input = JOptionPane.showInputDialog("Enter valid salary") salary <0 || salary > 250000)
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne,

Good morning I think its because I was tired last night and so frustrated when I was playing with the code. Seems as if I goofed some stuff up.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be making random changes to the code rather than paying attention to the suggestions that are being made.
Here you had the { } correctly positioned.
Here you had the while loop syntax and condition correct.
Combine those two correctly and you will have a while loop containing three lines of code - you then just need to swap the 2nd and 3rd lines round and you should be there.
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Brandon sir ... What I would like for you to do is see { } as if it was a page that represents a form you are making to be filled out. That means anything you are asking for must go on that page which is going to be between the { }

Example { How long have you been employed on your current job? }

If what you are trying to say goes outside the page { } How long have you been employed on your current job? then you are probably writing on the table or something and your form will remain with nothing on it.

Next I would like for you to see the while loop as the title of your form which conveys the purpose of the form.

So now we have the following:

while() <- This is your title
{ } <- This is your empty page

Now drawing back on the context of your problem... What you are really trying to get out of this is -- If the user enter a value less than 0 or more than 250000 then you are not satisfied with the answers supplied on the form, so you supply a copy of the form to see what the new response will be until you are satisfied with the response...

Now in terms of your problem we get the following:

while(salary < 0 || salary > 250000) <- Title: If you say your salary is less than zero or it is more than 25000 then I know you are lying and I wont accept your answer so you better answer correctly
{ <- Start of your page
salary = /* code for user to enter salary goes here */ <- Note: If you enter contrary to the title of this form you will be supplied a new form (So this loop will happen over and over until the instructions in the title is met)
} <- End of the page

And there you have it ... This logic can be supplied to any looping code once you make sure you are checking for the correct range of values or input within the condition of the loop... Now go try it pal ... I know you can do it
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!