• 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

while loop stuck

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to do the following and my code is putting me in a loop and not allowing me to input a valid entry after using an invalid #

I ask the user for input which contains a loop if the user doesnt use the correct information, however the loop doesnt allow the user to input if the entry fails the which statement

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try putting in System.out.println statement to see what the value of yearsOnJob is each time through the loop

--update--
i just read the post a little more closely.

Where exactly inside the loop would you expect it to get the input from the user?
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its not displaying all the lines of code



I have an input line could you be more specific about where I should put that line of code
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have edited your post to use code tags...edit it yourself and you can see what I added. It makes it MUCH easier to read.

I am not going to tell you where to put the line. I'm going to ask you a question...

What do you want to happen inside the loop?

 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the loop should have the following result

enter salary between 0 and 250000 then ask for years on job if invalid the loop will repeat statement

enter valid salary between 0 and 250000
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also, let me edit your code to be formatted better


your first do loop is lines 6-9. The only thing it does is print something. What ELSE should be inside that loop?
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have changed the code above before you did the thing to the post i think instead of doing the

do while loop i changed it to the while loop I'm going to use my program to execute this please give me a few minutes to see if its working
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused now it only does the println okay so does it not see the IF statement below or does it have to get included in the loop as well
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:I have changed the code above before you did the thing to the post


Please don't do that. If someone else were to come along, they would have a VERY hard time following the flow of the thread. If you make change, just put the updated version in a new post.

brandon mac wrote: i think instead of doing the do while loop i changed it to the while loop


You think that is the change you made? Surely you should know what change you made...

brandon mac wrote:so does it not see the IF statement below or does it have to get included in the loop as well


Again...what do you want the code to do?

You should stop writing java. turn off your computer. get some paper and pencils. write down what you want to happen, step by step, in English. be as detailed as you can. I'll even get you started.

Ask the user to input there years on the job.
Save the value user inputs.
Convert the inputted value from a string to a number
If the value is not good (for some definition of 'goog'
ask them again
get the input again
...etc...
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:I'm confused now it only does the println okay so does it not see the IF statement below or does it have to get included in the loop as well


If you are going to loop until the salary is within a given range then obviously you need some code in the loop that changes the value of salary.
However, even once you have code in the loop to change the value of salary, your loop condition is wrong. What value for salary will cause the condition on line 9 to be false and hence exit from the loop ?

The same applies to your yearsOnJob loop
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Joanne okay I think i can do the explanation piece i have thought about it and started to write it out on paper as you instructed.
declare variable

double salary
double yearsonjob

User provide input for salary
user provide input for years on job
loop input for salary if value is not between 0 and 250000
loop input for years on job if value is not between 0 and 50
if correct input detemine if meet criteria for loan
salary needs to = 30000
years on job needs to be at least 2

display you qualify for loan if don't meet criteria then display you don't qualify must meet 30000 salary and 2 years on job

is this what you were asking for this explain further what I'm trying to do but don't understand how you put this into java code in the end
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a start. No you revise it.

Forget EVERYTHING you wrote, except this one line:

loop input for salary if value is not between 0 and 250000


Can you expand it? What do you mean "loop input for salary"?

Pretend you are giving a 10 year old child directions. They need to be simple, clear, and complete.

So, expand that into 3-4 statements of what needs to be done.
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
loop input for salary if value is not between 0 and 250000

User Please input salary between value of 0 and 250000
if value not between 0 and 250000
print statement input valid salary between 0 and 250000
User input salary between value 0 and 250000

once loop criteria met with value of 0 to 250000

yearsonjob

user input yearsonjob between value of 0 and 50
if value not between 0 and 50
print statement input valid yearsonjob between 0 and 50
user input years on job

criteria met for yearson job

determine if user is eligible for loan
eligible for loan if
salary >= 30000
yearsonjob >-250000
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to focus on this part. That's what you should do too - focus on one small part, get it working, and get it working WELL before you try and do anything else.

loop input for salary if value is not between 0 and 250000

User Please input salary between value of 0 and 250000 //line 3
if value not between 0 and 250000
print statement input valid salary between 0 and 250000
User input salary between value 0 and 250000


I assume on line 3 you are actually saying "Print message to the user: Please input salary between 0 and 250,000"
The next thing you do is check to see if some value is between two numbers...but you never say you are going to get the input from the user.

Yes, I may be nit-picking, but the computer will be even worse. that's why i'm trying to get you to SLOW DOWN. so let's assume we correct that, and expand it a bit...

loop input for salary if value is not between 0 and 250000

ask user "Please input salary between value of 0 and 250000 "
get input from user
if value not between 0 and 250000


ask user "Please input salary between value of 0 and 250000 "
get input from user
if value not between 0 and 250000

ask user "Please input salary between value of 0 and 250000 "
get input from user
if value not between 0 and 250000
...


Aha!!! Now we can start to see something repeated over and over...almost like a loop...so maybe

do this {
ask user "Please input salary between value of 0 and 250000 "
get input from user
} over and over until the values is > 0 and the values is < 250000



See what that gets you...
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I just want to let you know that I really like how this forum is setup. You guys all make me push myself to understand what I'm trying to ask in english and then what I should be using for code language and this is really helping me actually learn what I need to do with it. So check this out I'm very excited and believe that I finally got the concept of where I need to be. Thanks again for all your help

// Get the user's annual salary.
input = JOptionPane.showInputDialog("Enter your " +
"annual salary.");
salary = Double.parseDouble(input);

while (salary >= 0 && salary <=250000);
{System.out.println("Enter the valid salary: ");
salary = Double.parseDouble(input);}
input = JOptionPane.showInputDialog("Enter valid salary");
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your while loop doesn't contain any code - see if you can work out why - it's a common typo.

Once you've fixed the above problem, I think your while loop condition is wrong - it will keep looping while salary is between 0 and 250000 - is that what you want ?

Why have you got System.out.println statements and JOptionPanes. Use one or the other.
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joanne,

Not sure what to do because when I run the code in my program to test it, everything seems to be working as it should the way i expected. Also thanks for letting me know about the: System.out and the JOption I will clean it up. It doesn't keep looping while between those #s what it does once it get the correct results it will move to ask years on job. I only posted the first part of the code because once I fixed it I did a copy and paste with the other variable. Would you like me to post the entire thing.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:It doesn't keep looping while between those #s what it does once it get the correct results it will move to ask years on job


Have you tried entering a valid value as the first value you enter ?

Or have you tried entering more than one invalid value ?

If so and they both work, then you need to repost your amended code because the above code won't handle these two situations correctly.
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks glad you pointed this out. I reran the program the error is occuring when i type the first set of values even if they are within the value for salary nothing happens after i enter a value above the 250000 for example it declares not valid and then i input a valid value and it works. This is strange let me dig around see if I can't figure this out. For some reason I think I'm missing an IF statement
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite clear on what you are trying to accomplish

But here is a possibly scenario:

1. Declare variables;

2. Use a do-while loop to request the first set of information (// Logic for getting valid range of salary) from the user where if the value the user entered is less than 0 or more than 250000 it will be checked in the condition and if it resolves to be true the user will be asked again to enter a valid range.

3. Use a do-while loop to request the second set of information ( // Logic for getting valid range for number of years on the current job) from the user where if the value the user entered is less than 0 or more than 50 it will be checked in the condition and if it resolves to be true the user will be asked again to enter a valid range.

4. Use an if statement to check for the values that your application requires which happens be to a salary amount of 30000 and at least 2 years on the current job, if the variables holds such values then it display qualified message else it display a denied message.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:This is strange let me dig around see if I can't figure this out.



Joanne Neal wrote:Your while loop doesn't contain any code - see if you can work out why - it's a common typo.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rico Felix wrote:Try this humble solution ...


Please see the quote at the top of the Java Beginners forum

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Rico Felix wrote:Try this humble solution ...


Please see the quote at the top of the Java Beginners forum

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.



Oops I missed that!
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joanne,

I'm not the type of person to give up or quit but I don't think I know where to go with this as I have went thru the while command and the if statements I'm only doing a while loop not a do while loop. Please push me in the right direction you said a common typo are you talking about the while command I tried to use the && instead of the || and that didn't fix it either.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:Joanne,

I'm not the type of person to give up or quit but I don't think I know where to go with this as I have went thru the while command and the if statements I'm only doing a while loop not a do while loop. Please push me in the right direction you said a common typo are you talking about the while command I tried to use the && instead of the || and that didn't fix it either.



Hey pal ... From reviewing the code you posted your problem is with your range signs > <

Take for instance if you want the range to be between 0 and 250000 you cannot say do /* get salary */ while salary > 0 || salary < 250000... that in itself is going against what you require

If the user types 2000 it will check 2000 > 0 = true or 2000 <= 250000 = true and will loop again when you really need it to move on

You should rather ask if the user types a number less than 0 salary < 0 then that is not valid and should be requested again for a valid value

Also if the user types a number greater than 250000 then that is not valid and should be requested again for a valid value
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a common mistake made when you switch from a do/while loop to a standard while loop.
In a do/while loop there is a semi-colon at the end because it is the end of a statement, but if you leave that semi colon in when you switch to the normal while loop then it becomes an empty statement controlled by the while loop and what you thought was controlled by the while loop, isn't

So what you actually have is a while loop containg just an empty statement and the the next three lines are not controlled by the while.
Remove the semi-colon and then you just need to get the logic of your while condition correct.

 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rico,

Thanks so much for taking a few minutes to explain that. So with the following code

which (salary >=0 salary <= 250000) this is causing the problem and instead your saying is get rid of the which statement and use an if statement ??

if(salary <0)
make user input again
if (salary >250000)
make user input again

Is this some what close
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joanne,

I removed the semi colon and reran it, however I think Rico has a valid point of the values that are contained in the which statement because its still not fully working. I also changed the or to the &&.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:Joanne,

I removed the semi colon and reran it, however I think Rico has a valid point of the values that are contained in the which statement because its still not fully working. I also changed the or to the &&.


He does indeed.
At the moment, you enter the while loop when the salary is between 0 and 250000. You actually want to enter it when the salary is outside that range.
Try
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:Joanne,

I removed the semi colon and reran it, however I think Rico has a valid point of the values that are contained in the which statement because its still not fully working. I also changed the or to the &&.



You are getting the concept ...

If you are requesting a range say between 25 and 40 then any value less than 25 is not in the range also any value over 40 is not in the range.

To get the user to enter a value within your specified range use a while loop in the case that the value is outside it will ask again for a valid value and only when the criteria is satisfied both members can agree and move on.
 
Greenhorn
Posts: 12
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:Joanne,
I tried to use the && instead of the || and that didn't fix it either.



You want to have a salary value between 0 and 250000.right?
but in the condition of the while loop you are checking (salary >= 0 && salary <=250000) which will loop for every value in the range. So,suppose your input in 5000, now that should be a valid input (isn't it??) but in your current logic it will still go into the loop and rerun the code.
So what you want is to check the conditions of your loop i.e you want a positive number for salary but also the value should not exceed 250000.
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm so close on this with all the help I have received but just not there yet. The which statement for the loop is still not working I feel that there is something else that can work better is this possible any suggestions.
 
Sam Kaydee
Greenhorn
Posts: 12
Android Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

brandon mac wrote:Joanne,
At the moment, you enter the while loop when the salary is between 0 and 250000. You actually want to enter it when the salary is outside that range.
Try




Did you try this??
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still don't work
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There have been numerous suggestions and presumably code changes since you last posted all of the code so please can you post the latest code you have so we can see exactly what you are now running.
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I believe there is still a problem with the way the loop is written because of the true status and it not moving forward
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at these lines in your code:

What do you think happens here?

You have a loop with one statement. Inside the loop, you don't change the value of the variable 'salary'. So, if the expression salary < 0 || salary > 250000 is true, the loop will repeat forever, printing "Enter the valid salary: " over and over again.

Another error in this line:

The ; is an empty statement. What this line means is: "as long as yearsOnJob is larger than zero or yearsOnJob is less than 50, do nothing".
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the user provides input for salary that is between the specific values it should then move to the next statement that will be displayed asking for the users input of years on the job, however if not inputted correctly i want the loop to take affect and ask for a valid salary
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Asking to input the salary should be inside the while loop, not after the while loop.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:When the user provides input for salary that is between the specific values it should then move to the next statement that will be displayed asking for the users input of years on the job, however if not inputted correctly i want the loop to take affect and ask for a valid salary


You had your { } positioned correctly before - why did you move the closing } ?

Edit : although you do need to swap the 2nd and 3rd line around - at the moment you are trying to calculate the salary before you have set the input variable from the JOptionpane.
 
reply
    Bookmark Topic Watch Topic
  • New Topic