My code is a payroll program that keeps track of an employee's first and last names, salary, and address. Whenever I try to run the program, I get the following error message:
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenize r.java:349)
at Payroll.<init>(Payroll.java:26)
at PayrollTest.main(PayrollTest.java:16)
Going off of this, I can assume that the problem lies with my String Tokenizer, but I can't seem to figure out exactly why. I think it might be because of the fact that the String Tokenizer reads from a file and that since the last line doesn't have anything on it it seems to be trying to read it and get a token from it, thus giving me an error. If anyone can point me in the right direction then it would be greatly appreciated. On a sidenote, does anyone know how to edit a post to say SOLVED by it? I ended up solving my own problem under the "Help me with my code" forum on this site and can't seem to figure out how to change it to SOLVED.
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; } }