Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am attempting to create a java program that reads employee data froma text file and produce a report. I get the following error when

I am attempting to create a java program that reads employee data froma text file and produce a report. I get the following error when running this code:

Java EmployeeSalary Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at EmployeeSalary.main(EmployeeSalary.java:94)

The text file is:

2014 Employee Smith,John 2000 2014 Employee Carrol,Paul 1500 2015 Employee Gorley,Erica 3200 2014 Salesman Greeley,Susan 4500 40000 2015 Salesman Jones,Bill 3000 70000 2014 Executive Bush,George 5000 55 2014 Executive Smith,Frederick 7500 55

The code is below:

import java.lang.*; import java.io.*; import java.util.*;

class Employee{ public String employeeName; public int enteredYear, employeeMonthlySalary; public Employee(int year, String name, int monthlySalary){ enteredYear = year; employeeName = name; employeeMonthlySalary = monthlySalary; } public int annualSalary(){ return employeeMonthlySalary *12; } public String toString(){ return "Employee Name: "+employeeName+", Monthly Salary: $"+employeeMonthlySalary; } }

class Salesman extends Employee{ public int employeeAnnualSales; public Salesman(int year, String name, int monthlySalary, int annualSales){ super(year, name, monthlySalary); enteredYear = year; employeeName = name; employeeMonthlySalary = monthlySalary; employeeAnnualSales = annualSales; } public int annualSalary(){ return super.annualSalary()+ (int)Math.min(20000,employeeAnnualSales*.2); } public String toString(){ return super.toString()+", Annual Sales: "+employeeAnnualSales; } }

class Executive extends Employee{ public int currentStockPrice; public Executive(int year, String name, int monthlySalary, int stockPrice){ super(year, name, monthlySalary); enteredYear = year; employeeName = name; employeeMonthlySalary = monthlySalary; currentStockPrice = stockPrice; } public int annualSalary(){ int bonus = 30000; return (currentStockPrice > 50)? super.annualSalary() + bonus : super.annualSalary(); } public String toString(){ return super.toString()+", Stock Price: "+currentStockPrice; } }

class Name{ public String firstName, lastName; public Name(String nameString){ firstName = nameString.split(",")[1]; lastName = nameString.split(",")[0]; } public String getName(){ return firstName+" "+lastName; } }

public class EmployeeSalary{ private static ArrayList employees2014 = new ArrayList<>(); private static ArrayList employees2015 = new ArrayList<>(); private final static String FILEPATH="/home/robert/Documents/Employees.txt"; private String year; private String empName; private String empSalary; public static void main(String[] args) throws IOException { // open file input stream Scanner fileScan = new Scanner(new File(FILEPATH)); fileScan.useDelimiter(" "); while (fileScan.hasNextLine()) { String line = fileScan.nextLine(); String[] tokens = line.split(" "); if (Integer.valueOf(tokens[0])==2014){ if (tokens[1]=="Employee"){ employees2014.add(new Employee(Integer.valueOf(tokens[0]), new Name(tokens[2]).getName(), Integer.valueOf(tokens[3]))); } else if (tokens[1]=="Salesman"){ employees2014.add(new Salesman(Integer.valueOf(tokens[0]), new Name(tokens[2]).getName(), Integer.valueOf(tokens[3]), Integer.valueOf(tokens[4]))); } else { employees2014.add(new Executive(Integer.valueOf(tokens[0]), new Name(tokens[2]).getName(), Integer.valueOf(tokens[3]), Integer.valueOf(tokens[4]))); } } else if (Integer.valueOf(tokens[0])==2015) { if (tokens[1]=="Employee"){ employees2015.add(new Employee(Integer.valueOf(tokens[0]), new Name(tokens[2]).getName(), Integer.valueOf(tokens[3]))); } else if (tokens[1]=="Salesman"){ employees2015.add(new Salesman(Integer.valueOf(tokens[0]), new Name(tokens[2]).getName(), Integer.valueOf(tokens[3]), Integer.valueOf(tokens[4]))); } else { employees2015.add(new Executive(Integer.valueOf(tokens[0]), new Name(tokens[2]).getName(), Integer.valueOf(tokens[3]), Integer.valueOf(tokens[4]))); } } } int averageSalary2014 = Integer.MIN_VALUE; int averageSalary2015 = Integer.MIN_VALUE; System.out.println("2014"); for (Employee employee2014 : employees2014){ System.out.println(employee2014.toString()); averageSalary2014 += employee2014.annualSalary(); } averageSalary2014 = averageSalary2014/employees2014.size(); System.out.println(" "+"2014 Average Employee Salary: "+ averageSalary2014); System.out.println("2015"); for (Employee employee2015 : employees2015){ System.out.println(employee2015.toString()); averageSalary2015 += employee2015.annualSalary(); } averageSalary2015 = averageSalary2015/employees2015.size(); System.out.println(" "+"2015 Average Employee Salary: "+ averageSalary2015); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions