This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes if statements and errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "if statements and errors" Watch "if statements and errors" New topic
Author

if statements and errors

pam rowell
Greenhorn

Joined: Jan 21, 2002
Posts: 7
if (income >= 0 ) && (income <=5000)
return 0;
if (income >= 5001) && (income <= 10000)
return .03;
if (income >= 10001) && (income <= 20000)
return .055;
if (income >= 20001) && (income <= 40000)
return .108;
if (income >=40001)
return .237;
is this a legit way to do an if statement. i'm a beginning java student and i got 13 error messages of this little bit of code. i have done everything but i can not fix it
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
You have a few things going on here.
First, the if's are looking to evaluate one statement into true or false. You need to surround your two statements with parenthesis to make them evaluate to one value (true or false). So do this:

Second, it would be good practice to use else�s. This becomes especially important with recursion and other situations. So do this:

Third, with regards to returns, some programmers feel that it�s more elegant and often easier to understand when only one return statement is used in any given method. Sometimes this is impractical, and it�s then best to use multiple return statements. The problem you are having is that there is no definite, concrete return statement. The compiler sees a bunch of if�s and considers: What if all of these are false, then what gets returned? In your case, nothing gets returned � which is an error. So, after all of your logic you could just have a return statement that returned some default value, like: return 0.0; But this is sloppy and bug prone. My suggestion is to use a local variable with some default value whose value is changed based upon the logic evaluations and then this variable is returned at the end. So do this:

Notice also, that stating clearly whether a decimal number is a float or a decimal, with the suffix f or d, is good practice � making it very clear what type of data is being used.
Hope some of this helps you.
Good Luck,
-Dirk Schreckmann
[ January 30, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Dirk Schreckmann:
Notice also, that stating clearly whether a decimal number is a float or a decimal, with the suffix f or d, is good practice – making it very clear what type of data is being used.

Why? In java all decimal numbers are doubles unless specified as floats. In this day and age there don't seem to be very many good reasons to use floats instead of doubles.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Well, accesses to doubles aren't atomic, so you may have safety issues in a multi-threaded environment.
Other than that though, I agree that doubles are just "the thing" to use.

Rob


Rob
SCJP 1.4
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, this digression is hardly "beginner" level, but what the heck. I think that depending on atomicity for thread safety is almost always a bad idea anyway. Folr starters, it only works if the variable in question is also volatile. And there's a very limited number of cases where you can actually do anything useful reliably using only atomic actions. You can't even increment a counter without another thread having an opportunity to screw it up, for Pete's sake. If you need thread safety, use synchronization, unless you really, really know what you're doing, and your application is so simple that you can get away with it.


"I'm not back." - Bill Harding, Twister
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
Marylin, Rob and Jim,
For those seeking certification your discussion on using doubles instead of floats and the reasons for it isn't very interesting, if not confusing.
Dirk gave a perfectly good answer to Pam's question I think, keeping in mind this is the place for 'beginners'...
Erik Dark
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Erik Dark:
For those seeking certification your discussion on using doubles instead of floats and the reasons for it isn't very interesting, if not confusing.
I'm sorry you feel that way, Erik. Dirk gave an excellent answer to Pam's question. However, especially since Pam is a beginner, she is probably very open to suggestions about style. I happen to believe that putting an 'f' or a 'd' after every floating point number in a program is not good style, and I was asking Dirk why he believes that it is necessary in Java.

Thread safety and synchronization are covered in the SCJP2 exam so "for those seeking certification" it is important to know, although I would have preferred to discuss this aspect in the Programmer's Certification forum since I would classify it a little above "beginner" level.

My apologies, Pam, for side-tracking your thread. I didn't know it would go this direction.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
OK - I will move this to Certification if you want to continue the conversation.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by pam rowell:



One more sidenote, Pam. I see that you're really testing to see where income falls within a range of valid values. The most common way I've seen this done is by checking the upper bounds and using an if-else chain (similar to the example given by Dick):

This is how I see this done most often. Notice that this is really almost exactly equivalent to what you have, but I find this more readable and I see this structure fairly often. One thing to note, however, is that the lower bound does not get checked in the initial if test. If the value has a lower bound limit, you may want to handle that in a "special case."
I hope that helps,
Corey


SCJP Tipline, etc.
 
 
subject: if statements and errors
 
Similar Threads
Rounding BigDecimal Number
procedures and functions
A mystery...
urgent help needed for assignment
Clumsy code