Basically, my code is a payroll program that keeps track of an employee's first and last names, salary, and address. I was doing fine up until I made my first object in the main class. My "Employee1" object seems to be the trouble, and I thought it might have been because I didn't have the .txt file in the right folder. I copied the .txt file to every folder of the project because this is my first time using NetBeans and it made so many folders I had no idea which one to put it in. Long story short, if I don't comment out everything involving "Employee1" then the program doesn't run. It gave me something along the lines of "no such element exception" if I remember correctly. I wish I could get the exact phrase but NetBeans won't even let me compile the program right now, so I can't. If anyone can help point me in the right direction it would be greatly appreciated.
import java.util.*; import java.io.*; public class PayrollTest { public static void main(String[] args)throws IOException { Payroll Employee1 = new Payroll("payrollSample.txt"); Scanner keyboard = new Scanner(System.in); 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"); String input = keyboard.nextLine(); if (input.equals("a")) addEmployee(); } public static void addEmployee(){ Scanner keyboard = new Scanner(System.in); 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(); System.out.println("What is the employee's address?"); String address = keyboard.nextLine(); boolean success = Employee1.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."); } }
import java.util.*; import java.io.*; public class Payroll { private ArrayList<Employee> payList; public Payroll(){ payList = new ArrayList<Employee>(); } public Payroll(String filename)throws IOException{ payList = new ArrayList<Employee>(); Scanner fileInput = new Scanner(new File(filename)); while (fileInput.hasNextLine()){ String line = fileInput.nextLine(); StringTokenizer st = new StringTokenizer(line, "~"); String firstName = st.nextToken(); String lastName = st.nextToken(); double salary = Double.parseDouble(st.nextToken()); String address = st.nextToken(); payList.add(new Employee(firstName, lastName, salary, address)); } } private int search(String firstName, String lastName){ int i = 0; 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(); } public void printPayRoll(){ int i = 0; 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; } }