Roxanne Roxanne - 1 month ago 6
Java Question

Trying to compute payroll in java

I have to figure out if gross pay is between "so and so" it's "this" tax percentage, etc. I thought I was doing alright, but it keeps outputting every single tax answer as one answer if I enter a high number for hours worked... like this "Deductions are 275.0165.0770.0000000000001".

Am I also doing this an extremely long way because I'm overthinking?
Thanks so much for any help!

import java.util.Scanner;
public class prob2
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);

double range = (168);

System.out.println("Enter the number of hours worked in a week:");
double hours = in.nextDouble();

System.out.println("Enter rate per hour:");
double rate = in.nextDouble();

double overtimeHours = hours - 40;
double overtimePay = (overtimeHours * rate) * 1.5;
double basePay = (hours - overtimeHours) * rate;
double grossPay = basePay + overtimePay;
double socialSecurity = .1 * grossPay;
double medical = .06 * grossPay;

if (overtimeHours < 0 )
{
System.out.println("Number of overtime hours are " + 0);
}
else
{
System.out.println("Number of overtime hours are " + overtimeHours);
}

if (overtimeHours < 0 )
{
System.out.println("Base pay is " + hours * rate);
}
else
{
System.out.println("Base pay is " + basePay);
}

if (overtimeHours < 0 )
{
System.out.println("Overtime pay is " + 0);
}
else
{
System.out.println("Overtime pay is " + overtimePay);
}

if (grossPay < 0 )
{
System.out.println("Gross pay is " + hours * rate);
}
else
{
System.out.println("Gross pay is " + grossPay);
}

if (grossPay > 0 && grossPay < 43)
{
System.out.println("Deductions are " + socialSecurity + medical);
}
else

if (43.01 < grossPay && grossPay < 218.00)
{
System.out.println("Deductions are " + socialSecurity + medical + (.10 * grossPay));
}
else

if (218.01 < grossPay && grossPay < 753.00)
{
System.out.println("Deductions are " + socialSecurity + medical + (.15 * grossPay));

}
else

if (grossPay > 0 && 753.01 < grossPay && grossPay < 1762.00)
{
System.out.println("Deductions are " + socialSecurity + medical + (.25 * grossPay));

}
else

if (1762.01 < grossPay && grossPay < 3627.00)
{
System.out.println("Deductions are " + socialSecurity + medical + (.28 * grossPay));

}
}
}

Answer

Please wrap your sum into parenthesis:

System.out.println("Deductions are " + (socialSecurity + medical));

In this case it will create sum at first then concatenate result to string, otherwise it will concat socialSecurity then medical one by one.

The same rule is right for similar cases in your code.