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]);
}
}
}
}
You've got a problem with your
for
loops, specifically yourj
loop and youri
loop. Thei
loop is inside thej
loop, and it really looks as though it shouldn't be. You should haveThe 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.