I can't seem to get my program to remove the object that appears at a certain index of my ArrayList, nor can I use a search method and display an object at a certain index in my ArrayList. My program is meant to add a new employee to the arraylist if they don't already exist, and then have the options to remove an employee, find an employee, print out everything from the arraylist, and then quit. I think there may be something wrong starting with add, because after doing so I print out the arraylist and they show up on the display, but when I try to remove an employee I get my own error message "Employee does not exist" and when I try to find an employee I get the following error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(ArrayList.java:371) at java.util.ArrayList.get(ArrayList.java:384) at Payroll.findAllInfo(Payroll.java:82) at PayrollTest.findEmployee(PayrollTest.java:94) at PayrollTest.main(PayrollTest.java:35)
In a nutshell, when I add someone and print up the payroll then that person shows up with everyone else, but when I try to remove that person they no longer exist, and then when I try to find anyone, even the ones that were already on the file thats being read in, I get the above error message. I'm at a loss as to what to do, but the problem seems to be stemming from one line, I just can't seem to figure out whats wrong with said line. My code is below, and I've commented the appropiate line I'm referring to.
import java.io.*; import java.util.*; public class PayrollTest { public static void main(String[] args)throws IOException { Scanner keyboard = new Scanner(System.in); Payroll storePayroll = new Payroll("payrollSample1.txt"); String input; do { System.out.println("Please choose a function below." + "\n\na - add new " + "employee\nr - remove employee from the address book\nf - " + "find an employee's information\np - print out payroll\nq " + "- quit program"); input = keyboard.nextLine(); if (input.equals("a")) { addEmployee(storePayroll, keyboard); } else if (input.equals("r")) { removeEmployee(storePayroll, keyboard); } else if (input.equals("f")) { findEmployee(storePayroll, keyboard); } else if (input.equals("p")) { storePayroll.printPayRoll(); } }while(!input.equals("q")); } public static void addEmployee(Payroll storePayroll, Scanner keyboard){ System.out.println("What is the employee's first name?"); String firstName = keyboard.nextLine(); System.out.println("What is the employee's last name?"); String lastName = keyboard.nextLine(); System.out.println("What is the employee's salary?"); double salary = keyboard.nextDouble(); keyboard.nextLine(); System.out.println("What is the employee's address?"); String address = keyboard.nextLine(); boolean success = storePayroll.add(firstName, lastName, salary, address); if (success == true) { System.out.println("The employee has been added."); } else { System.out.println("The employee is already in the system."); } } public static void removeEmployee(Payroll storePayroll, Scanner keyboard){ System.out.println("What is the employee's first name?"); String firstName = keyboard.nextLine(); System.out.println("What is the employee's last name?"); String lastName = keyboard.nextLine(); boolean success = storePayroll.remove(firstName, lastName); if (success == true) { System.out.println("The employee has been removed."); } else { System.out.println("The employee is not in the system."); } } public static void findEmployee(Payroll storePayroll, Scanner keyboard){ System.out.println("What is the employee's first name?"); String firstName = keyboard.nextLine(); System.out.println("What is the employee's last name?"); String lastName = keyboard.nextLine(); System.out.println(storePayroll.findAllInfo(firstName, lastName)); } }import java.io.*; import java.util.*; public class Payroll { private ArrayList<Employee> payList; public Payroll(){ payList = new ArrayList<>(); } public Payroll(String filename)throws IOException{ payList = new ArrayList<>(); Scanner fileInput = new Scanner(new File(filename)); while (fileInput.hasNextLine()){ String line = fileInput.nextLine(); //System.out.println("line: " + line); StringTokenizer st = new StringTokenizer(line, "~"); String firstName = st.nextToken(); String lastName = st.nextToken(); double salary = Double.parseDouble(st.nextToken()); String address = st.nextToken(); //String[] tokens = line.split("~"); //String firstName = tokens[0]; //System.out.println(firstName); //String lastName = tokens[1]; //System.out.println(lastName); //double salary = Double.parseDouble(tokens[2]); //System.out.println(salary); //String address = tokens[3]; //System.out.println(address); payList.add(new Employee(firstName, lastName, salary, address)); } } private int search(String firstName, String lastName){ int i; Employee tempEmployee = new Employee(firstName, lastName, 0.0, null); for (i = 0; i < payList.size(); i++){ if (payList.get(i).equals(tempEmployee)) { return i; } } return -1; } public boolean add(String firstName, String lastName, double salary, String address){ int index = search(firstName, lastName); if (index == -1){ payList.add(new Employee(firstName, lastName, salary, address)); return true; } else { return false; } } public boolean remove(String firstName, String lastName){ int index = search(firstName, lastName); if (index == -1) { return false; } return true; } public String findAllInfo(String firstName, String lastName){ int index = search(firstName, lastName); if (index == -1) { System.out.println("The Employee you requested does not work here"); } return payList.get(index).toString(); //****This is the line indicated by the error message****// } public void printPayRoll(){ int i; for (i = 0; i < payList.size(); i++){ System.out.println(payList.get(i)); } } }public class Employee { static int empcount = 0; private int empID = 0; private String firstName = null; private String lastName = null; private String address = null; private double salary = 0.0; public Employee(){ } public Employee(String fname, String lname, double sal, String addr){ firstName = fname; lastName = lname; address = addr; salary = sal; } public int getID(){ return empID; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public String getAddress(){ return address; } public double getSalary(){ return salary; } public void setFirstName(String fname){ firstName = fname; } public void setLastName(String lname){ lastName = lname; } public void setAddress(String addr){ address = addr; } public String toString(){ return firstName + "~" + lastName + "~" + salary + "~" + address; } public boolean equals (Object obj){ return false; } }