Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

My program is supposed to calculate the income of the user by taking the number of hours worked and their hourly wage. if the person works over 40 hours, thy recieve 1.5 times the amount of their usual hourly wage. for some reason, when 41 hours is entered with \$10 per hour, the program outputs \$425 when it should output \$415. got any ideas why??
```import java.util.Scanner;

public class WageCalculator {

public static void main(String[] args) {
Scanner scan  = new Scanner(System.in);
double hoursworked=0, hoursworked40 = 0, wage, totalpay;
int i = 0;
System.out.println("Enter number of hours worked: ");
hoursworked = scan.nextInt();
System.out.println("Enter hourly wage: ");
wage = scan.nextInt();
while (hoursworked >= 0 && hoursworked <= 168 && i < 1){
if(hoursworked > 40){
hoursworked40 = hoursworked - 40;
hoursworked40 = hoursworked40 * 1.5;
totalpay = (hoursworked + hoursworked40) * wage;
System.out.println("Total pay: \$" + totalpay);
i+=1;
}else{
totalpay = hoursworked * wage;
System.out.println("Total pay: \$" + totalpay);
i+=1;
}
}
if(hoursworked < 0 || hoursworked > 168)
System.out.println("Your hours need to be between 0 and 168.");
}
}```

got any ideas why??
Which line(s) of code is computing the wrong value?
Add a println() after each line that assigns a value to a variable and print out the variable's value.
If you know how the computations are supposed to go, you should be able to look at the output and see where the problem is.

"totalpay = (hoursworked + hoursworked40) * wage;"
In that line it calculates the extra hour both in hoursworked(41) and hoursworked40(1.5). You should make "hoursworked=40;" before that line.

4. ## The Following User Says Thank You to bad.sector For This Useful Post:

gotdatdough (November 3rd, 2013)

Originally Posted by Norm
Which line(s) of code is computing the wrong value?
```hoursworked40 = hoursworked - 40;
hoursworked40 = hoursworked40 * 1.5;
totalpay = (hoursworked + hoursworked40) * wage;```
These lines are.

--- Update ---

Thank you. i knew it was somehting simple!