• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if-else statement

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think you mean

Or don'e even put a condsition there - just use
 
Marshal
Posts: 79234
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please post your new code once again.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic