Java ArrayList越界异常?工资类别

Java ArrayList越界异常?工资类别,java,arraylist,Java,Arraylist,我在编程类中有一个项目,它必须给出一个工资场景,其中包含三个类,包括PayRoll、PayRollTester和EmployeeRecord。我的代码正确编译和打印EmployeeRecord,但没有正确列出工资单。相反,我收到一个错误,它是error:outboundsofArrayList 工资单: import java.util.Scanner; import java.util.ArrayList; import java.util.Date; // ...javadoc... pu

我在编程类中有一个项目,它必须给出一个工资场景,其中包含三个类,包括PayRoll、PayRollTester和EmployeeRecord。我的代码正确编译和打印EmployeeRecord,但没有正确列出工资单。相反,我收到一个错误,它是error:outboundsofArrayList

工资单:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;

// ...javadoc...
public class Payroll
{
    //ArrayLists that will use methods from EmployeeRecords.
    ArrayList<String> employeeNames2;
    ArrayList<Double> employeeWages2;

    ArrayList<Integer> emID = new ArrayList<Integer>();
    ArrayList<Double> hours = new ArrayList<Double>();
    ArrayList<Double> totalPay = new ArrayList<Double>();
    //Creating the hours and wages variables.
    private double hoursWorked = 0.0;
    private double hoursWorked2 = 0.0;
    private int weeks = 0;
    private String employeeID = "%03d";
    private int quit = 1000;
    private int i = 1;

    Scanner input = new Scanner(System.in);

    public void setEmployeePayroll()
    {
        // Constructs a new EmployeeRecord.
        EmployeeRecord e = new EmployeeRecord();
        e.setEmployeeInfo();

        employeeNames2 = e.getEmployeeNamesArrayList();
        employeeWages2 = e.getWageArrayList();

        // Local variables used in setEmployeePayroll.
        double totalPay2 = 0.0;
        double totalHours = 0.0;
        double overTime = 0.0;
        double overTime2 = 0.0;

        System.out.println("Please enter ACME employee ID, the week they worked (1 or 2), and the number of hours worked. This information should be entered in the order the names were entered. Enter 0 when you are done inputing information.");

        while(quit != 0)
        {
            quit = input.nextInt();
            if(quit == 0)
            {
                break;
            }

            weeks = input.nextInt();

            if(weeks == 1)
            {
                hoursWorked = input.nextDouble();
            }
            else if(weeks == 2)
            {
                hoursWorked2 = input.nextDouble();
            }

            /*
             * I am checking to see if the employee is going to be paid for overtime and also calculating the pay for both weeks.
             * 1) My first if statement indicates whether or not the employee worked over 40 in week one and week two.
             * 2) My first else if statement indicates whether the employee works more than 40 hours in week two but not week one.
             * 3) My second else if statement indicates whether the employee works more than 40 hours in week one but not in week two.
             * 3) My third else if statement finally indicates that the employee worked over 40 hours in both week one and two.
             */
            if(hoursWorked > 0 && hoursWorked <= 40 && hoursWorked2 > 0 && hoursWorked2 <= 40)
            {
                totalHours = hoursWorked + hoursWorked2;
                hours.add(totalHours);
                totalPay2 = totalHours * (employeeWages2.get(i - 1));
                totalPay.add(totalPay2);
                hoursWorked = 0.0;
                hoursWorked2 = 0.0;
            }
            else if(hoursWorked2 > 40 && hoursWorked > 0 && hoursWorked <= 40)
            {
                overTime2 = hoursWorked2 - 40;
                totalHours = hoursWorked + hoursWorked2;
                hours.add(totalHours);
                totalPay2 = totalHours * (employeeWages2.get(i - 1)) + (overTime2 * 1.5);
                totalPay.add(totalPay2);
                hoursWorked = 0.0;
                hoursWorked2 = 0.0;
            }
            else if(hoursWorked > 40 && hoursWorked2 <= 40 && hoursWorked2 > 0)
            {
                overTime = hoursWorked - 40;
                totalHours = hoursWorked + hoursWorked2;
                hours.add(totalHours);
                totalPay2 = totalHours * (employeeWages2.get(i - 1)) + (overTime * 1.5);
                totalPay.add(totalPay2);
                hoursWorked = 0.0;
                hoursWorked2 = 0.0;
            }
            else if(hoursWorked > 40 && hoursWorked2 > 40)
            {
                overTime = hoursWorked - 40;
                overTime2 = hoursWorked2 - 40;
                totalHours = hoursWorked + hoursWorked2;
                hours.add(totalHours);
                totalPay2 = totalHours * (employeeWages2.get(i - 1)) + (1.5 * (overTime + overTime2));
                totalPay.add(totalPay2);
                hoursWorked = 0.0;
                hoursWorked2 = 0.0;
            }
            i = quit;
        }
        System.out.println();
        System.out.println("Employee Number  |  Employee Name    |  Hours Worked  |  Total Pay");
        for(int i = 0; i < e.getEmployeeNamesArrayList().size();)
        {
            System.out.println(String.format(employeeID, i + 1) + "              | " + emID.get(i + 1) + "        | " + hours.get(i + 1) + "           | " + totalPay.get(i + 1));
        }
    }
}
import java.util.Scanner;
import java.util.ArrayList;

// ...javadoc...
public class EmployeeRecord
{
    /*
     * Creating the array and instance variables for EmployeeRecord consisting
     * of TaxID numbers, Employee Names, Wages, Employee ID numbers and hours worked.
     */
    ArrayList<String> employeeNames = new ArrayList<String>();
    ArrayList<String> taxIDList = new ArrayList<String>();
    ArrayList<Double> employeeWages = new ArrayList<Double>();

    Scanner input = new Scanner(System.in);

    private String employeeID = "%03d";
    private String employeeFullName = " ";
    private String taxID = " ";
    private double wage = 0.0;

    //Adding employees.
    public void setEmployeeInfo()
    {
        System.out.println("Please enter the full names of each ACME employee, their employee tax ID, and their employee wage rate. Type 'Q Q' when you are done entering employee information.");

        while(employeeFullName != "Q")
        {
            employeeFullName = input.next() + " " + input.next();
            if(employeeFullName.equalsIgnoreCase("Q" + " " + "Q"))
            {
            break;
            }
            taxID = input.next();
            wage = input.nextDouble();

            employeeNames.add(employeeFullName);
            taxIDList.add(taxID);
            employeeWages.add(wage);

            System.out.println("Employee ID  |  Employee Name        |  Tax ID          |  Wage");
            for(int i = 1; i <= employeeNames.size(); i++)
            {
                System.out.printf(String.format(employeeID, i) + "          | " + employeeNames.get(i - 1) + "              | " + taxIDList.get(i - 1) + "          | " + "%1.2f",employeeWages.get(i - 1));
                System.out.println();
            }
        }
    }

    /**
     * Creating a method that returns the employee ArrayList employeeName.
     */
    public ArrayList<String> getEmployeeNamesArrayList()
    {
        return employeeNames;
    }

    /**
     * Creating a method that returns the employee's Tax ID ArrayList taxIDList.
     */
    public ArrayList<String> getTaxIdsArrayList()
    {
        return taxIDList;
    }

    /**
     * Creating a method that returns the wages ArrayList 
     */
    public ArrayList<Double> getWageArrayList()
    {
        return employeeWages;
    }

}
import java.util.Scanner;

// ...javadoc...
public class PayrollTester
{
    public static void main(String[] args)
    {
        Payroll employeeComplete = new Payroll();
        employeeComplete.setEmployeePayroll();
    }
}

ArrayList索引从0开始。因为for循环的开始i=0,所以在访问arrayList元素时不能执行(i+1)

emID.get(i + 1)

而且emID的大小始终为0,因为您刚刚创建了新的Arraylist,但没有向该列表添加任何对象。

Arraylist索引从0开始。因为for循环的开始i=0,所以在访问arrayList元素时不能执行(i+1)

emID.get(i + 1)

而且emID的大小始终为0,因为您刚刚创建了新的Arraylist,但没有向该列表中添加任何对象。

while(employeeFullName!=“Q”)
应该是
while(!employeeFullName.equals(“Q”)
这里的代码比我们真正需要的多得多。请发布一个简短但完整的程序来演示这个问题-请参阅-并确保您也包含堆栈跟踪。
while(employeeFullName!=“Q”)
应该是
while(!employeeFullName.equals(“Q”)
这里的代码远远超出了我们的实际需要。请发布一个简短但完整的程序来演示这个问题-请参阅-并确保包括堆栈跟踪。