0

I am building something for a class Project, the code is still messy, please ignore that. The Question i am asking is how to fix this Error:

===================================

Employee Name | Naofumi

Hours Worked | 40

Hourly Rate | 9.75

Employee Name | // NOTICE here that is skips the input question "Employee name"

Hours Worked |

===================================

/// CODE:-----

import java.util.Scanner;

public class PayRollProject {
     public static void main(String[] args) {

        final double FEDERALTAX = .18;
        final double STATETAX = .045;
        final double HOSPITALIZATION = 25.65;
        final double UNIONDUES = 7.85;

        // Init. Variable
        Scanner Board = new Scanner(System.in);

        String[] name = new String[3];
        int[] hourWages = new int[3];
        double[] hourRate = new double[3];
        double[] grossPay = new double[3];
        double[] fedTax = new double[3];
        double[] stateTax = new double[3];
        double[] deductions = new double[3];
        double[] netPay = new double[3];



        //GP = HW * HR;
        //FW = GP * .18;
        int i, j, k;

        // Back Door

        for(k = 0; k < 3; k++) {
            System.out.println();

            System.out.println("Employee Name |");

            name[k] = Board.nextLine();

            System.out.println("Hours Worked |");

            hourWages[k] = Board.nextInt();

            System.out.println("Hourly Rate |");

            hourRate[k] = Board.nextDouble();

            System.out.println();
            System.out.println();
        }

        // input/ calculations

        for(j = 0; j < 3; j++) {
    /*      System.out.println();
            System.out.println("Employee Name |");
            name[j] = Board.nextLine();
            System.out.println("Hours Worked |");
            hourWages[j] = Board.nextInt();
            System.out.println("Hourly Rate |");
            hourRate[j] = Board.nextDouble();      */

            grossPay[j] = hourWages[j] * hourRate[j];
            fedTax[j] = grossPay[j] * .18;
            stateTax[j] = grossPay[j] * .045;
            netPay[j] = grossPay[j] - (fedTax[j] + stateTax[j] + HOSPITALIZATION + UNIONDUES);

            for(i = 0; i < 3; i++) {
                System.out.println("Employee    |           " + name[i]);
                System.out.println("Hours Work  |           " + hourWages[i]);
                System.out.println("Hourly Rate |           " + hourRate[i]);
                System.out.println("Gross Pay   |           " + grossPay[i]);
                System.out.println("");  //- < Blank!
                System.out.println("Deductions:                    ");
                System.out.println("Federal Withholding |          " + fedTax[i]);
                System.out.println("State WithHolding |            " + stateTax[i]);
                System.out.println("Hospitalization |              " + HOSPITALIZATION);
                System.out.println("Union Dues |                   " + UNIONDUES);
                System.out.println("                               -----");
                System.out.println("Total Deductions |             " + deductions[i]);
                System.out.println("                               ");
                System.out.println("NetPay |                       " + netPay[i]);
            }
        }
    }
}
Leigh Flix
  • 198
  • 1
  • 7

2 Answers2

0

You've got a problem with your for loops, specifically your j loop and your i loop. The i loop is inside the j loop, and it really looks as though it shouldn't be. You should have

for(j = 0; j < 3; j++) {
/*  System.out.println();
    System.out.println("Employee Name |");
    name[j] = Board.nextLine();
    System.out.println("Hours Worked |");
    hourWages[j] = Board.nextInt();
    System.out.println("Hourly Rate |");
    hourRate[j] = Board.nextDouble();      */

    grossPay[j] = hourWages[j] * hourRate[j];
    fedTax[j] = grossPay[j] * .18;
    stateTax[j] = grossPay[j] * .045;
    netPay[j] = grossPay[j] - (fedTax[j] + stateTax[j] + HOSPITALIZATION + UNIONDUES);
}

// the loop above is complete; now we start the next one

for(i = 0; i < 3; i++) {
    System.out.println("Employee    |           " + name[i]);
    System.out.println("Hours Work  |           " + hourWages[i]);
    System.out.println("Hourly Rate |           " + hourRate[i]);
    System.out.println("Gross Pay   |           " + grossPay[i]);
    System.out.println("");  //- < Blank!
    System.out.println("Deductions:                    ");
    System.out.println("Federal Withholding |          " + fedTax[i]);
    System.out.println("State WithHolding |            " + stateTax[i]);
    System.out.println("Hospitalization |              " + HOSPITALIZATION);
    System.out.println("Union Dues |                   " + UNIONDUES);
    System.out.println("                               -----");
    System.out.println("Total Deductions |             " + deductions[i]);
    System.out.println("                               ");
    System.out.println("NetPay |                       " + netPay[i]);
}

The reason this is causing you trouble is that you're printing output for the second and third employees when you've only calculated results for the first one.

chiastic-security
  • 19,689
  • 3
  • 33
  • 62
0

I guess this issue is due to the use of nextLine. Either use a dummy nextLine before actually reading the input. This will read and flush the input after nextxxx and then read fresh. Or you may try using next instead.

I hope that helps. There is also a same problem here

Community
  • 1
  • 1
Parth Satra
  • 505
  • 2
  • 15