• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Issues with if else statement

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

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.


I hope someone can help!

Thanks in advance
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Linda Hi!

First thing that caught my eye was:

What if 'projectedAnnual' equaled exactly 6000? Which 'if' stmt do you want to be followed? (<= 6000 or >= 6000)?

Since 'projectedAnnual' tops out at 150000, perhaps that should be the first amount tested, no?

With an ascending set of values, your code will never get to anything past the 'greater-than' 6000... cuz it's all greater than 6000, i.e., 25000 > 6000, 75000 > 6000, 150000 > 6000.

Flip the sequence & you'll be a happy camper!
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

e.g.:

 
Linda Selfridge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Drop line 50 & you're on your way.
 
Linda Selfridge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I will give it a go.

Cheers
 
Ranch Hand
Posts: 100
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it safe to assume that hoursWorked number is at least 38?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe that implies that you're also penalized at time-and-a-half if you don't wok a minimum of 38 hours

Good catch, Ilya!
 
this is supposed to be a surprise, but it smells like a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic