-2

I'm trying to replace payRate * hoursWorked into a single variable grossPay but when I do it's returning a value of 0.0 for any calculation. double grossPay is commented out on purpose so you can see what I was trying to do.

import java.text.*;
import java.util.Scanner;



public class elseIfPayroll{

        public static void main(String[] args){


        double hoursWorked =0.00;
        double payRate =0.00;
        int dependents =0;



        Scanner keyboard = new Scanner(System.in);
        NumberFormat f = new DecimalFormat("#0.00");


            System.out.print("Enter pay rate: ");
            payRate = keyboard.nextInt();

            System.out.print("Enter hours worked: ");
            hoursWorked = keyboard.nextInt();

            System.out.print("Enter amount of dependents: ");
            dependents = keyboard.nextInt();

            double grossPay= (payRate * hoursWorked);

//gross pay decision statements

        if (hoursWorked<=40) {
            System.out.println("Total $ Earned: " + payRate * hoursWorked); 
            }

            else if (hoursWorked >= 40) {
                System.out.println("Total $ Earned: " + payRate * hoursWorked +(payRate * 0.5));
                }

            else if (hoursWorked>=60){
                System.out.println("Total $ Earned: " + payRate * hoursWorked + (payRate * 2));
                }






//else if statements for dependents

        if (dependents == 0){
                    f.format(payRate * hoursWorked);
            }

            else if (dependents == 1){
                    f.format(payRate * hoursWorked - (payRate *                                         hoursWorked*0.04) );
            }

            else if (dependents == 2){
                     f.format(payRate * hoursWorked - (payRate *                                                hoursWorked*0.0775));
            }

            else if (dependents == 3){
                    f.format(payRate * hoursWorked - (payRate *                                                 hoursWorked*0.1125) );
            }

            else if (dependents == 4){
                    f.format(payRate * hoursWorked - (payRate *                                                 hoursWorked*0.145) );
            }

            else if (dependents == 5){
                    f.format(payRate * hoursWorked - (payRate *                                                 hoursWorked*0.175) );
            }

            else if (dependents >= 6){
                    f.format(payRate * hoursWorked - (payRate *                                                 hoursWorked*0.2025) );
            }


            System.out.println("New gross Pay: " + f.format(payRate * hoursWorked - (payRate *                                      hoursWorked*0.04)));




    }
}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
montenegroxnj
  • 23
  • 1
  • 2
  • 4
  • 3
    If youre multiplying and it turns out to 0, one of the factors is 0 because there are no zero divisors in the reals. – G. Bach Feb 04 '13 at 03:11
  • 1
    I'm not sure what the problem is. Both multiplication operands are zero at the point of the commented out multiplication, so of course it is going to get a zero result. I think it may be time for an [sscce](http://sscce.org/) – Patricia Shanahan Feb 04 '13 at 03:12
  • Did you already debugged your program? If not, please do it and find your problem. – Luiggi Mendoza Feb 04 '13 at 03:13
  • I'm really new to java an I have no idea how to debug.. I just want to know how to put payRate and hoursWorked into one variable .. grossPay – montenegroxnj Feb 04 '13 at 03:18

1 Answers1

7

In Java - and many other languages - doing

result = operand1 operator operand2

calculates the result immediately. Java doesn't keep in memory the program line in result that would update grossPay as soon as hoursWorked or payRate are modified, but only the double result.

So when you

    double hoursWorked =0.00;
    double payRate =0.00;
    double grossPay= (payRate * hoursWorked);

grossPay takes the result of 0 * 0 (ie zero).

You just have to move a bit the grossPay calculation

        System.out.print("Enter pay rate: ");
        payRate = keyboard.nextInt();

        System.out.print("Enter hours worked: ");
        hoursWorked = keyboard.nextInt();

        System.out.print("Enter amount of dependents: ");
        dependents = keyboard.nextInt();

        double grossPay= (payRate * hoursWorked);

to make it to work.

Déjà vu
  • 28,223
  • 6
  • 72
  • 100
  • 3
    @montenegroxnj: I think you should accept this answer. Also, from the change log it appears that you altered your original code in order to reflect what ring0 posted; I think you should have left the original (wrong) code unchanged instead. – damix911 Feb 09 '13 at 23:25