I am attempting to create a java program that reads employee data froma text fil
ID: 3805305 • Letter: I
Question
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<Employee> employees2014 = new ArrayList<>();
private static ArrayList<Employee> 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);
}
}
Explanation / Answer
i don't see any for loop to initalize the variables.you can do something like this,for Tokens[ ]
Now you can modify it to initialize your table with values as per your assignment. But what happen if you replace condition i < SIZE with i < 11? Well, you will get IndexOutOfBoundException, as you try to access (at some point) an object under index 10, but the highest index in 10-element array is 9. So you are trying, in other words, to find friend's home with number 11, but there are only 10 houses in the street.
down vote
i don't see any for loop to initalize the variables.you can do something like this,for Tokens[ ]
for(i=0;i<5;i++){ /* Code which is necessary with a simple if statement*/ } final int SIZE = 10; int[] array = new int[SIZE]; for (int i = 0; i < SIZE; i++) { array[i] = i; } Now you can modify it to initialize your table with values as per your assignment. But what happen if you replace condition i < SIZE with i < 11? Well, you will get IndexOutOfBoundException, as you try to access (at some point) an object under index 10, but the highest index in 10-element array is 9. So you are trying, in other words, to find friend's home with number 11, but there are only 10 houses in the street.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.