Deepend
Deepend

Reputation: 4155

Wrongly executing both "if" and "else" in While loop in Java

I’m just really learning Java, going through tutorials in book "Java how to Program" by Deitel and Deitel, so please forgive any basic mistakes I’m making. I understand my methodology might not be the best but I hope to improve this.

My problem is I believe I have constructed my program wrongly. Both the IF and ELSE options are being outputted when I execute the program.

If anyone could tell me why both options are executing it would be very much appreciated

Deepend

package controlStatments;
import java.util.Scanner;

public class Review_4_20 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        //Declare Variables
        double employeeOneRate;
        double employeeOneHours;
        double employeeTwoRate;
        double employeeTwoHours;
        double employeeThreeRate;
        double employeeThreeHours;
        int calculator;
        double employeeOneTotalPay;
        double employeeOneNormalPay;
        double employeeOneTotalPayOverTime;
        double overTimeRate;

        //Initiate Variables
        employeeOneRate = 0;
        employeeOneHours = 0;
        employeeTwoRate = 0;
        employeeTwoHours = 0;
        employeeThreeRate = 0;
        employeeThreeHours = 0;
        calculator = 0;
        employeeOneTotalPay = 0;
        employeeOneTotalPayOverTime = 0;
        overTimeRate = 1.5; 
        employeeOneNormalPay = 0;

        //Create While Loop
        while (calculator != -1)
        {   
            //Prompt user to input details
            System.out.print("\n\nPlease input the first employees rate");
            employeeOneRate =input.nextDouble();

            System.out.print("Please input the first employees Hours");
            employeeOneHours =input.nextDouble();

            if (employeeOneHours <= 40)
            {
                employeeOneTotalPay = employeeOneHours * employeeOneRate;
                System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
            }
            else 
                employeeOneNormalPay = employeeOneRate * 40;
            employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
            System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
        }
    }
}

Upvotes: 0

Views: 1956

Answers (3)

Rafiq
Rafiq

Reputation: 2000

Change

if (employeeOneHours <= 40)
        {
            employeeOneTotalPay = employeeOneHours * employeeOneRate;
            System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
        }
        else 
            employeeOneNormalPay = employeeOneRate * 40;
        employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
        System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);

With:

if (employeeOneHours <= 40)
        {
            employeeOneTotalPay = employeeOneHours * employeeOneRate;
            System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
        }
        else 
       {
            employeeOneNormalPay = employeeOneRate * 40;
        employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
        System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
}

Upvotes: 1

Makoto
Makoto

Reputation: 106430

Bracketed statements, such as if, else if, else, and the loops only execute the line immediately following it if there is no bracket afterwards.

So, your statement:

if (employeeOneHours <= 40)
{
    employeeOneTotalPay = employeeOneHours * employeeOneRate;
    System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
}
else 
    employeeOneNormalPay = employeeOneRate * 40;
    employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
    System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);

always multiplies employeeOneNormalPay.

Change it to:

if (employeeOneHours <= 40) {
    employeeOneTotalPay = employeeOneHours * employeeOneRate;
    System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
} else {
    employeeOneNormalPay = employeeOneRate * 40;
    employeeOneTotalPayOverTime = (employeeOneHours - 40) * (employeeOneRate * overTimeRate) + employeeOneNormalPay;                            
    System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
}

Upvotes: 1

alain.janinm
alain.janinm

Reputation: 20065

You forgot the bracket in your else statement.

I supose it should be like this :

    if (employeeOneHours <= 40)
    {
        employeeOneTotalPay = employeeOneHours * employeeOneRate;
        System.out.printf("\nNormal time pay is: %.2f", employeeOneTotalPay);
    }
    else {
        employeeOneNormalPay = employeeOneRate * 40;
        employeeOneTotalPayOverTime = (employeeOneHours - 40) * 
                (employeeOneRate * overTimeRate) + employeeOneNormalPay;                                
        System.out.printf("\n\nTotal Pay including Overtime is: %.2f", employeeOneTotalPayOverTime);
    }

If you don't set brackets only the first line is include in the IF/ELSE statement.

Upvotes: 6

Related Questions