-2

I have this code for a ManageEmployees class for my COP 2800 school class and it says that there is an error in case 2

import java.util.ArrayList;
import java.util.Scanner;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.io.*;

public class ManageEmployees {

    public static void main(String[] args) throws IOException {
        java.io.File file = new java.io.File("employee.txt");
        java.io.PrintWriter Manage = new java.io.PrintWriter(file);
        ArrayList<Employee> employee = new ArrayList<Employee>(4);
        Scanner emp = new Scanner(System.in);
        String firstName, lastName, hireDate;
        LocalDate date;
        double payRate;
        boolean x = false;
        boolean a = false;
        boolean b = false;
        boolean c = false;
        boolean d = false;
        do {
            System.out.println("Enter you choice:");
            System.out.println("1 - Add Employees");
            System.out.println("2 - Display Employees");
            System.out.println("3 - Exit");
            String choice = emp.nextLine();
            switch(choice) {
            case "1":
                do {
                System.out.println("Enter the first name.");
                firstName = emp.nextLine();
                if(firstName.length() == 0) {
                    System.out.println("No value detected! Please enter a value.");
                    a = true;
                }
                else
                {
                    Manage.println(firstName);
                    a = false;
                }
                }while(a != false);
                do {
                    System.out.println("Enter the last name.");
                    lastName = emp.nextLine();
                    if(lastName.length() == 0) {
                        System.out.println("No value detected! Please enter a value.");
                        b = true;
                    }
                    else
                    {
                        Manage.println(lastName);
                        b = false;
                    }
                }while(b != false);
                do {
                    System.out.println("Enter the hire date.");
                    hireDate = emp.nextLine();
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
                    date = LocalDate.parse(hireDate, formatter);
                    if(hireDate.length() == 0) {
                        System.out.println("No value detected! Please enter a value.");
                        c = true;
                    }
                    else
                    {
                        Manage.println(hireDate);
                        c = false;
                    }
                }while(c != false);
                do {
                    System.out.println("Enter the pay rate.");
                    payRate = emp.nextDouble();
                    if(payRate == 0) {
                        System.out.println("No value detected! Please enter a pay rate.");
                        d = true;
                    }
                    else
                    {
                        Manage.println(payRate);
                        d = false;
                    }
                    
                }while(d != false);
                Employee add = new Employee(firstName, lastName, date, payRate);
                employee.add(add);
                x = true;
                break;
            case "2":
                StringBuilder E = new StringBuilder();
                for(int idx = 0; idx <= employee.size(); idx++) {
                    E.append("[firstName = " + employee.get(idx) +", lastName = " + employee.get(idx) + ", hireDate = " + employee.get(idx) + ", payRate = " + employee.get(idx) + "]");
                }
                System.out.println("class edu.seminolestate.manageemployees.Employees " + E);
            }
        }while(x != false);
    }


}

it continues to say this

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:373)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at edu.SeminoleState.ManageEmployees.ManageEmployees.main(ManageEmployees.java:94)
halfer
  • 18,701
  • 13
  • 79
  • 158

1 Answers1

0

You need to test for index < size() not less than or equal. The indexes run from zero to size -1

Toby Eggitt
  • 1,686
  • 15
  • 20