Brittney Bailey
Brittney Bailey

Reputation: 51

Java if Else Statement Error

I am attempting to determine based off of input from the user which formula to calculate. It works when they are both closed off with {} but when the entire statement is closed then my System.out.print does not recognize them. Currently there are no brackets around the if statement. and it is stating that totalSalary is already defined in the main string. Thank you for your time and input.

    if(annualSales >= 96000)
    {
     double totalSalary = annualPay + advCommission;
    if(annualSales <= 95999)
    { 
     double totalSalary = annualPay + commission;



    //prints table for display   
    //Columns
    System.out.print("SalesPerson\t");
    System.out.print("AnnualSales\t");
    System.out.print("Commission\t "); 
    //Columns
    System.out.print("Total Salary\t");System.out.println("Difference from user");

    //user input
    System.out.print(firstName);System.out.print("\t\t");
    System.out.println(annualSales); System.out.print(commission); 
    System.out.print(totalSalary);System.out.println("Difference from user");
    //preset 1
    System.out.print(presetSalesPerson[0]);
    System.out.print("\t\t");
    System.out.println(presetAnnualSales[0]);             System.out.print("Commission\t "); 
    System.out.print(totalSalary);System.out.println("Difference from user");
//preset 2
    System.out.print(presetSalesPerson[1]);System.out.print("\t\t");
     System.out.println(presetAnnualSales[1]);   System.out.print("Commission\t "); 
    System.out.print(totalSalary);System.out.println("Difference from user");

Upvotes: 2

Views: 213

Answers (2)

CubeJockey
CubeJockey

Reputation: 2219

It says totalSalary is already defined because you cannot create the variable twice (which is what you're attempting in your if statements). In addition to that issue, I suggest you fix the braces and try again:

 double totalSalary = 0;

 if(annualSales >= 96000){
     totalSalary = annualPay + advCommission;
 }
 if(annualSales <= 95999){
     totalSalary = annualPay + commission;
 }

Or, in a better format:

double totalSalary = 0;

if(annualSales >= 96000){
    totalSalary = annualPay + advCommission;
}
else{
    totalSalary = annualPay + commission;
}

Pulling your double totalSalary out of the if statements will also let your SysOuts 'see' the variable.

Upvotes: 9

Prerak Sola
Prerak Sola

Reputation: 10019

Try like this:

double totalsalary = 0;
if(annualSales >= 96000)
{
    totalSalary = annualPay + advCommission;
}

if(annualSales <= 95999)
{
    totalSalary = annualPay + commission;
}



    //prints table for display   
        //Columns
    System.out.print("SalesPerson\t");System.out.print("AnnualSales\t"); System.out.print("Commission\t "); 
        //Columns
    System.out.print("Total Salary\t");System.out.println("Difference from user");

    //user input
    System.out.print(firstName);System.out.print("\t\t");System.out.println(annualSales); System.out.print(commission); 
    System.out.print(totalSalary);System.out.println("Difference from user");
    //preset 1
    System.out.print(presetSalesPerson[0]);System.out.print("\t\t");System.out.println(presetAnnualSales[0]); System.out.print("Commission\t "); 
    System.out.print(totalSalary);System.out.println("Difference from user");
    //preset 2
    System.out.print(presetSalesPerson[1]);System.out.print("\t\t");System.out.println(presetAnnualSales[1]); System.out.print("Commission\t "); 
    System.out.print(totalSalary);System.out.println("Difference from user");

Upvotes: 3

Related Questions