• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to take different actions for different ranges of data  RSS feed

 
kirk jimenez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My professor like us to make a salary, taxes and etc exercise. which we should type the name first then, input salary. then if we input the amount 8000 or higher. the system will ask another question about the marital status. and that is it.

My real problem is when we input 5000-7999 the system should ask a different question, stating do you want to pay for your premiums and a follow up of yes and no choices. if we input 0-4999 the system will just state that it is your current salary.... etc..



this is my code i hope you can help me please!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

I don't see how any of this has anything to do with NetBeans, so I'll move it to a more appropriate forum.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kirk jimenez wrote:My professor like us to make a salary, taxes and etc exercise. which we should type the name first then, input salary. then if we input the amount 8000 or higher. the system will ask another question about the marital status. and that is it.

Actually, it isn't. You need to write down exactly what needs to happen for each range that you're interested in; and I suggest you do it in English (or your native language) on a piece of paper BEFORE you start writing any code. Either that or copy ALL your instructions down and post them here.

Second: You need to be very careful with ranges:
if (salary > 8000 && salary < 10000)
does NOT include salary == 8000.
I also suggest you space out your code better; you'll find it easier to read (and so will we ).

Otherwise, if...else if...else if... is the normal way to do this.

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good advice from Winston. If you are using if else‑if else‑if else‑if else‑if else‑if else, then make all the conditions run in order and look the same.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I hate all that if else-if else-if ... code. Just something about it. I would wrap it all in a switch statement. To me it just looks cleaner and is easier to read/modify/update. Your mileage may vary.

Regards,
Robert
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I hate switch statements because I always have to look up the correct syntax and because I always have to put "break" after each of the pieces. Besides, switch doesn't work well for Campbell's example:


 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both else‑if and switch have their place, particularly since in Java7 you can use Strings after case.
 
kirk jimenez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here is my reworked codes. I tried to follow the "from lowest number to highest number" and tried to separate 1000-4999 problem. and I quite made it work. but when I tried 1000 and below I get no response ( good ) but won't end the run test. and that happend to 1000 - 4999 and 5000 - 7999 still need to fix my problem please help me. I'm dying here
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not done making all the test exclusive, there are quite a few cases still to be fixed. For example, lines 19-21 stand on their own. Lines 23-25 stand on their own. And it looks like lines 41-54 are not part of the correct IF statement.

If you're not sure which lines of code are being executed, I suggest putting lots of System.out.println statement in the code so you know exactly what is or is not getting executed.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kirk jimenez wrote: . . .
Nononononononononononono That will not work, because you are missing 1000 and 5000.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kirk jimenez wrote: . . . please help me. I'm dying here
We are helping. You have to do your part. Anybody would think you had never read any of our replies.
 
kirk jimenez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:You're not done making all the test exclusive, there are quite a few cases still to be fixed. For example, lines 19-21 stand on their own. Lines 23-25 stand on their own. And it looks like lines 41-54 are not part of the correct IF statement.

If you're not sure which lines of code are being executed, I suggest putting lots of System.out.println statement in the code so you know exactly what is or is not getting executed.


is there anything wrong with my if and else statement? if yes please do tell me
and what do you mean by lines 19-21 stand alone? and etc. thank you
 
kirk jimenez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
kirk jimenez wrote: . . . please help me. I'm dying here
We are helping. You have to do your part. Anybody would think you had never read any of our replies.


yes sir I know Im doing my part I have tried the replies. but it seems I can't understand it well. please be brief I'm a newbie sorry and thank you for your patience
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there anything wrong with my if and else statement?

Yes, there are several problems. I suggest the following: Put a System.out.println statement after EVERY SINGLE ONE of the statements in your code. Then you'll know real fast where it's going wrong. You will have to add more brackets "{ ... }" where there aren't any now , but you should do that anyway. The code could look like this:


etc.
 
kirk jimenez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try this I'll post later thank you sir
 
kirk jimenez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
is there anything wrong with my if and else statement?

Yes, there are several problems. I suggest the following: Put a System.out.println statement after EVERY SINGLE ONE of the statements in your code. Then you'll know real fast where it's going wrong. You will have to add more brackets "{ ... }" where there aren't any now , but you should do that anyway. The code could look like this:


etc.


still not working (((
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what have you changed? And more importantly, what have you learned about the cases where your code is wrong by observing what code gets executed? How many System.out.println statements have you placed? You need at least 12 to cover even the major cases in your code.

Have you tried it with 1000? Have you tried it with 5000? Have you tried it with 4000? I'm fairly certain the code is wrong for all of those. You need to find out where the code goes wrong; if you can't do it by reading the code, then extensive use of System.out.println is the next best approach.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does “not working” mean?

And we knew it would not work correctly, because we can see you have not corrected any of the errors.
 
kirk jimenez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys I haven't replied fast I was out doing something else. and by the way I tried putting S.O.P in each range but still I can't figure out my mistakes here is my revised code I am so close I know it. please can you tell me exactly what is wrong with my codes. please please please

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still have the same errors, I am afraid.
 
kirk jimenez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You still have the same errors, I am afraid.


could you please point it out.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you have made a number of changes, there's nothing fundamentally different about it - the same kinds of errors are still present, which suggests that you have not learned much from putting all those println statements in the code. Why did you remove them before posting, by the way? It would be instructive for you to explain to us what output they produce, and why.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!