Mo Marks
Mo Marks

Reputation: 3

How to use an "if" statement

Im trying to insert an "if" statement in this code but Its not working to well. Its a fixed salary and an incentive(37.28 * 1.32) will be give once 94,261.02 is made. Making less than 94,261.02 is just a regular 37.28 commission. All iny "int" lines are underlined in red with red ! marks. So I'm trying to figure the problem:

        System.out.println("Enter your annual sales");
        String annual = input.nextLine();

        int salary = 7550281; 
        int commission = 38_28; 
        int compensation = Integer.parseInt(annual) * commission + salary;
        System.out.println("compensation is: "+compensation );

        if (Integer.parseInt(annual) < 92416_02) {
          int salary = 7550281;
          int commission = 37_28 * 1_32;
          int compensation = Integer.parseInt(annual) * commission + salary;
          System.out.println("compensation is: "+compensation );

        } else if (Integer.parseInt(annual) > 92416_02){
         int salary = 7550281; 
         int commission = 38_28; 
         int compensation = Integer.parseInt(annual) * commission + salary;
         System.out.println("compensation is: "+compensation );
        }   

Thanks.

Upvotes: 0

Views: 218

Answers (4)

soniccol
soniccol

Reputation: 23

I modified your code , since you didn't provide complete code block , I did the code segment by my imagination. I would recommend you to use BigDecimal class for more precise calculation. '

import java.util.Scanner;        
public class stack_overflow {
    public static void main(String args[]){
        System.out.println("Enter your annual sales");
        Scanner input = new Scanner(System.in);
        String annual = input.nextLine();
        double salary = 7550281; 
        double commission = 38.28; 
        double compensation = Double.parseDouble(annual) * commission + salary;
        System.out.println("compensation is: "+compensation );

        if (Double.parseDouble(annual) < 92416.02) {
           salary = 7550281;
           commission = 37.28 * 1.32;
           compensation = Double.parseDouble(annual) * commission + salary;
           System.out.println("compensation is: "+compensation );

        } else if (Integer.parseInt(annual) > 92416.02){
          salary = 7550281; 
          commission = 38.28; 
          compensation = Integer.parseInt(annual) * commission + salary;
          System.out.println("compensation is: "+compensation );
    }
}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347194

A lot will come down to which version of Java you are using.

For the moment, let's assume you are using Java 7 and 38_28 is a valid statement, you are redeclaring your variables within each if block

// Declared here...
int salary = 7550281;
int commission = 38_28;
int compensation = Integer.parseInt(annual) * commission + salary;
if (Integer.parseInt(annual) < 92416_02) {
    // Redeclared here...
    int salary = 7550281;
    int commission = 37_28 * 1_32;
    int compensation = Integer.parseInt(annual) * commission + salary;
} else if (Integer.parseInt(annual) > 92416_02) {
    // Redeclared here...
    int salary = 7550281;
    int commission = 38_28;
    int compensation = Integer.parseInt(annual) * commission + salary;
}

This is not required. You only need to declare them once, for example...

int salary = 7550281;
int commission = 38_28;
int compensation = Integer.parseInt(annual) * commission + salary;
if (Integer.parseInt(annual) < 92416_02) {
    salary = 7550281;
    commission = 37_28 * 1_32;
    compensation = Integer.parseInt(annual) * commission + salary;
} else if (Integer.parseInt(annual) > 92416_02) {
    salary = 7550281;
    commission = 38_28;
    compensation = Integer.parseInt(annual) * commission + salary;
}

I think you'll also be safer using long over int to prevent any possible overflow

Nit Pick

You are also, repeatedly, converting the annual value. While there is little wrong with it, it does tend to clutter the code and makes it a little difficult to read. It would suggest converting it once and simply re-using the resulting value, for example...

int annualAmount = Integer.parseInt(annual);
if (annualAmount < 92416_02) {
    //...
    compensation = annualAmount * commission + salary;
} else if (annualAmount > 92416_02) {
    //...
    compensation = annualAmount * commission + salary;
}

Upvotes: 2

Oleg Pyzhcov
Oleg Pyzhcov

Reputation: 7353

Your code suffers from the following problems:

  1. Local variables duplication (salary, compensation and commission are declared twice). If you want to assign value to already existing variable, you shouldn't specify a type (int here) before the variable name.
  2. Your multiplication is invalid. 37_28 * 1_32 gives 492096. Underscores don't matter at all. You probably have to divide your result by 100 to give it logical sence.
  3. Your code doesn't handle the case when the annual is exactly 92416_02. Remove the else if clause and initialize your commission instead, or just use else without following if. Also, since there are so many lines common, you could just move them out of if block.

Note also that the user has to input their annual multiplied by 100 since parseInt won't recognize underscores. Otherwise, this code can probably do what you want: System.out.println("Enter your annual sales"); String annual = input.nextLine();

    int salary = 7550281;
    int commission = 38_28;
    if (Integer.parseInt(annual) < 92416_02) {
      commission = 37_28 * 1_32 / 100;
    }
    int compensation = Integer.parseInt(annual) * commission / 100 + salary;
    System.out.println("compensation is: "+compensation );

P.S. Don't listen to people suggesting floats or doubles for calculations with money - this is a bad, error-prone practice because calculation errors accumulate over time. Use int, long, BigInteger or BigDecimal (with String constructor)

Upvotes: 0

OneOfOne
OneOfOne

Reputation: 99224

Use floats and . instead of _, eg : float commission = 38.28;

Upvotes: 0

Related Questions