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
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 ]
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
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.
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
Joined: Jul 22, 2000
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.
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