This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am very new to java programming and am trying to get an if else statement to work in relation to calculating taxation amounts.
Basically I need it to say: If salary is less than XXXX then tax = 0, otherwise if salary is greater than or equal to XXXXX then tax = (calculation), otherwise if salary is greater than or equal to XXXXX then tax = (calculation) etc
But at the moment it is only calculating on the first level of the calculation.
I really need help! My issue is between lines 38 & 47.
Well, one problem is that the tests are going to be executed in order, but they all overlap. For example, if projectedAnnual is 100,000, the program is first going to check if it's less than or equal to 6000, which it's not, but then it's going to check if it's greater than or equal to 6000, which it is, so that piece of code will be executed, and that's it -- no more "else" clauses will be considered, since one was already satisfied. You need to either re-order them, or turn the tests around to be "less than" rather than "greater than", so the smaller tests won't apply for the bigger numbers.
You could accomplish what you're trying to do without them, but you could use multiple tests in your if statements. An advantage is that readability is improved, a disadvantage is that it's adding complexity, perhaps unnecessarily.
Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Joined: Mar 26, 2011
You guys are legends!
I did what you said and flipped the statements around and it now calculated properly! YAY! YAY! YAY! I was up till 4am this morning trying to fix the issue!
I am forever in everyones debt!
For future reference the code now looks like this....
Thank you again!
hypGnosis von Schwarz
Joined: Oct 01, 2005
Linda, Hello again!
Hope you check back, because I have one additional comment.
You should remove the statement at line 50: if (projectedAnnual <= 6000)
and simply let the tax bracketing fall through to: yearTax = 0.00 ... the default tax amount
Besides that, what if the taxable amount is exactly 6000? It is referenced with equal signs in both lines 47 and 50.