• Post Reply Bookmark Topic Watch Topic
  • New Topic

if-else statement  RSS feed

 
Danny Takeda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,

I am new to Java Programming, and I am having trouble getting my if else statements to function properly. I can get the first if statement to function and my first else if statement works. However, I cannot get the final else if statement to function properly with the correct commission rate. Can someone direct me in the right direction and tell me where i am going wrong. Thank you.

 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think you mean

Or don'e even put a condsition there - just use
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch

Lots of people get confused about < and >, so don't worry about it. You have got far too much code in your main method, which dhou‍ld contain a single statement ideally.
Remove those comments which don't tell anybody anything they don't already know. There is no point in following, “please enter name,“ with a comment saying the name is being entered.
Don't use decimal format classes; use printf as described here instead. You probably want %6.2f for money.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!
These are conditions:
  • If annualSales < 96000 then CommissionRate = 0
  • If annualSales > 96000 and annualSales <= 120000 then CommissionRate = 0.05
  • If annualSales > 120000 then CommissionRate = 0.065


  • For last condition, if first condition fails means annualSales which is not less than 96000 means greater than 96000. If second also fails means annualSales not less or equals to 120000 means annualSales is greater than 120000 so for that no need to check as we know it is greater than 120000.


  • Few changes IMO:
  • Don't write that much code in main method creates confusion and also hard to find logical errors, so better create a separate method you can name it getComissionCalculater() or anything you feel more expressive.
  • Worth Reading Main Is A Pain and Java Programming Style Guide
  • Don't declare variables as static unless there is really specific reason for it. I don't think you need to declare salary and Percentage as static.
  • Variable name starts with small letters. I think rather than Percentage, percentage would look nice.
  • final variable names are written in capital letters. SALARY rather than salary makes you realize it is final variable.
  • No need to create another Scanner object here, already created on line no 26.

  •  
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If second also fails means annualSales not less or equals to 120000
    Edit: I meant annualSales not less and equals to 120000
     
    Danny Takeda
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Everyone,

    First thank you for the responses i have made the changes that everyone has suggested but I still cannot get the final "else" statement to initialize. More suggestions would be appreciated.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please post your new code once again.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!