user3344826
user3344826

Reputation: 1

Anyone know why these if else statement wont work?

public class Salary
{
    public static void main (String[] args)
    {
        double currentSalary; // employee's current salary
        double raise; // amount of the raise
        double percentRaise;   // percentage of the raise
        double newSalary; // new salary for the employee
        String rating; // performance rating

        Scanner scan = new Scanner(System.in);

        System.out.print ("Enter the current salary: ");
        currentSalary = scan.nextDouble();

        System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
        rating = scan.next();
        if (rating.equals("Excellent"))

            percentRaise = .06;   
            raise = (.06 * currentSalary);


            else if (rating.equals("Good"))

                    percentRaise = .04;
                   raise = (.04 * currentSalary); 


                   else  if (rating.equals("Poor"))

                       percentRaise = .015;
                       raise = (.015 * currentSalary);

        //Compute the raise using if ...

        newSalary = currentSalary + raise;

        //Print the results
        NumberFormat money = NumberFormat.getCurrencyInstance();
        System.out.println();
        System.out.println("Current Salary: " + money.format(currentSalary));
        System.out.println("Amount of your raise: " + money.format(raise));
        System.out.println( "Your new salary: " + money. format (newSalary) );
        System.out.println();
        scan.close();
    }
}

if i add { and } where the whitespace is then it says raise is not initialized. No matter what i do i cant seem to figure out to get it running. Right now it tells me to delete the else to let it run but if i do no matter i write excellent, good, or poor. It does .015 * salary so i cant get excellent or good to run.

Upvotes: 0

Views: 1529

Answers (4)

Baahubali
Baahubali

Reputation: 4802

You have to make sure that if your if statement contains more than one line of code, it is wrapped in braces.

public class Salary
 {
public static void main (String[] args)
{
    double currentSalary; // employee's current salary
    double raise; // amount of the raise
    double percentRaise;   // percentage of the raise
    double newSalary; // new salary for the employee
    String rating; // performance rating

    Scanner scan = new Scanner(System.in);

    System.out.print ("Enter the current salary: ");
    currentSalary = scan.nextDouble();

    System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
    rating = scan.next();
    if (rating.equals("Excellent"))
         {
        percentRaise = .06;   
        raise = (.06 * currentSalary);

         }
        else if (rating.equals("Good"))
        {
                percentRaise = .04;
               raise = (.04 * currentSalary); 

          }
               else  if (rating.equals("Poor"))
       {
                   percentRaise = .015;
                   raise = (.015 * currentSalary);
          }
    //Compute the raise using if ...

    newSalary = currentSalary + raise;

    //Print the results
    NumberFormat money = NumberFormat.getCurrencyInstance();
    System.out.println();
    System.out.println("Current Salary: " + money.format(currentSalary));
    System.out.println("Amount of your raise: " + money.format(raise));
    System.out.println( "Your new salary: " + money. format (newSalary) );
    System.out.println();
    scan.close();
}

}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

if (rating.equals("Excellent"))
    percentRaise = .06;   
    raise = (.06 * currentSalary);
else if (rating.equals("Good"))

Won't compile because Java sees this as...

if (rating.equals("Excellent"))
    percentRaise = .06;   

raise = (.06 * currentSalary);

else if (rating.equals("Good"))

Meaning that the compiler will complain about the else-if without a if statement.

The other problem you're having (when you place { } around the statements) is because Java makes no determination about what the initial value of a local variable will have.

This means Java simply doesn't know what to do with newSalary = currentSalary + raise; as there is no guarantee that raise will have a value assigned to it.

You could overcome this by adding an else condition to the end of your if-else block or simply supplying an initial value to your local variables...

    double currentSalary = 0; // employee's current salary
    double raise = 0; // amount of the raise
    double percentRaise = 0;   // percentage of the raise
    double newSalary = 0; // new salary for the employee
    String rating = ""; // performance rating

And while it might seem annoying, it's better then getting some completely random value which you would have to spend time trying to debug ;)

Updated

Remember, String#equals is case sensitive, this means "Excellent" is not equal to "excellent".

You could use String#equalsIgnoreCase instead

Upvotes: 2

nunoh123
nunoh123

Reputation: 1217

You can only write if statements without braces if it contains only one command, if you have more than one command (more than one line) you need to enclose those commands in braces

Upvotes: 0

user3316752
user3316752

Reputation:

You need to wrap if statements into these things ---> "{}"

so its like this:

if(statement){
   //then do someting
}else{
   //then do something else
}

Upvotes: 0

Related Questions