# Thread: Percentages in Switch Case Statement

1. Member
Join Date
Sep 2015
Posts
4
Rep Power
0

## Percentages in Switch Case Statement

This is some of the code I am working on. My main problem is that even if I enter a number that is higher than 10,000.00 it still will give the commissionRate as 5.0% instead of 7.5%. Can anyone help me? Thanks

System.out.printf("Greetings %s! What is your current status as an employee?%n", userName);
System.out.println("Enter 'P' for part-time and 'F' for full-time.");
status = input.nextLine();
status = status.toUpperCase();

switch (status){
case "P":
if (totalSales >= 0.0);
commissionRate = 3.5;
break;

case "F":
if (10000.00 > totalSales)
commissionRate = 5.0;

else
commissionRate = 7.5;
break;
}

Excerpt from console run:
(Your total sales profit for this month's payment period is 120000.0.
You said that you status was F.
Based upon this, your commission rate is 5.0%. {Should be 7.5!}
Using this rate, you total commission earned is 6000.0.)

2. ## Re: Percentages in Switch Case Statement

Two comments on this piece of code, neither of which solve your problem, but I thought I'd mention it:
1) The first if (in case "P") is not doing what you think it does. Statement commisionRate=3.5 will ALWAYS be executed, due to the semi-colon behind the if statement. The if block is empty. Make it a habit to enclose if and else parts in curly braces, so you want fall in this trap.
2) A comparison like this: 10 > variabele is making my brain read that twice. Don't make my brain work. It would be easier to read and less error prone to say variable < 10. Other coders might disagree, just sayin'...

Java Code:
```System.out.printf("Greetings %s! What is your current status as an employee?%n", userName);
System.out.println("Enter 'P' for part-time and 'F' for full-time.");
status = input.nextLine();
status = status.toUpperCase();

switch (status) {
case "P":
if (totalSales >= 0.0) {
commissionRate = 3.5;
}
break;
case "F":
if (totalSales < 10000) {
commissionRate = 5.0;
}
else {
commissionRate = 7.5;
}
break;
}```
As for your issue, it doesn't appear to be in the switch part. Can you post the entire piece of code so we can run and test it?

3. ## Re: Percentages in Switch Case Statement

Originally Posted by SurfMan
The if block is empty. Make it a habit to enclose if and else parts in curly braces, so you want fall in this trap.
That doesn't guarantee anything :)
Java Code:
```if (condition); {
doSomething();
}```
Compiles, runs without error, just doesn't do what was evidently intended.

db

4. ## Re: Percentages in Switch Case Statement

Originally Posted by DarrylBurke
That doesn't guarantee anything :)
Java Code:
```if (condition); {
doSomething();
}```
Compiles, runs without error, just doesn't do what was evidently intended.

db
You are correct. Dammit. Let's rephrase that into: semi-colons on the same line where an if statement is, is a code-smell. :)

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Percentages in Switch Case Statement

Of course, for part-time work, why even test for total sales? How can you have negative sales?

Regards,
Jim

6. Member
Join Date
Sep 2015
Posts
4
Rep Power
0

## Re: Percentages in Switch Case Statement

/**
* FILE : SalesCommissionPayment
* PURP : Calculate how much commission a salesman earns
* @author Sarah Janaskie
*
*/

import java.util.Scanner;

public class SalesmenCommissionPayment {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

//USER INFORMATION
String userName = "";
double totalSales = 0.0;
String status = "";

//COMMISSION INFORMATION
double commissionRate = 0.0;
double commissionEarned = 0.0;
double companyProfit = 0.0;

System.out.println("Welcome valued employee! Here at ACME Hammers Inc., it is our goal to not only take care of our customers but also you.");
System.out.println("Without you we would have no customers! This is why we offer you a commission.");
System.out.println("Within just a couple of questions, we will acurately be able to calculate this for you.");

System.out.println("To get started, please enter your name (first and last) so that your commission can be successfully transferred to your bank account.");

System.out.printf("Greetings %s! What is your current status as an employee?%n", userName);
System.out.println("Enter 'P' for part-time and 'F' for full-time.");
status = input.nextLine();
status = status.toUpperCase();

switch (status){
case "P":
if (totalSales >= 0.0);
commissionRate = 3.5;
break;

case "F":
if (totalSales < 10000.00)
commissionRate = 5.0;

else if (totalSales >= 10000.00)
commissionRate = 7.5;
break;
}

System.out.println("Finally, what is the total sales for this months time period?");
totalSales = input.nextDouble();

commissionEarned = (commissionRate * totalSales) / 100;
companyProfit = totalSales - commissionEarned;

System.out.printf("Thank you for your answers %s. We have now successfully calculated your commission earned.%n", userName);
System.out.println("See below:");

System.out.printf("Your total sales profit for this month's payment period is %.1f.%n", totalSales);
System.out.printf("You said that you status was %s.%n", status);
System.out.printf("Based upon this, your commission rate is %.1f%%.%n", commissionRate);
System.out.printf("Using this rate, you total commission earned is %.1f.%n", commissionEarned);

System.out.printf("Thank you for you hard work %s! Your efforts have earned our company %.1f.", userName, companyProfit);
System.out.println("This money will be put to good use to grow our company so that we can provide better service to our customers and to you!");

}

}

7. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Percentages in Switch Case Statement

You reposted your code but didn't ask a question or fix the problem reported by others. And please place your code between code tags for proper formatting.
Read about it here -> BB Code List - Java Programming Forum - Learn Java Programming

Regards,
Jim

8. Member
Join Date
Sep 2015
Posts
4
Rep Power
0

## Re: Percentages in Switch Case Statement

My apologies, one of the administrators had requested that i post my entire code so that they could run it and test it. Next time i will use the code tags.

9. ## Re: Percentages in Switch Case Statement

one major issue is nowhere your totalsales value your are receiving.its always initialized value 0.0.check once.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•