The program compiles fine, but when I run it, I get an error, more specifically this:
java.lang.ArrayIndexOutOfBoundsException: 1
It is getting the error on:
String name = array[1];
I am not sure why.
This is the code with the problem:
infile = new Scanner(new FileReader("EmployeeData.TXT"));
while(infile.hasNext()){
String line = infile.nextLine();
String array[] = line.split(":");
String name = array[1];
String id = array[2];
double salary = Double.parseDouble(array[3]);
Employee e;
if (array[0].equals("s")){
e = new SalariedWorker(id, name, salary);}
else {
boolean overtime = Boolean.parseBoolean(array[4]);
if(overtime){
int maxhu = Integer.parseInt(array[5]);
e = new HourlyWorker(id, name, salary, maxhu);
}
else{
e = new HourlyWorker(id, name , salary);
}
}
company.add(e);
}
For reference, this is the rest of the program:
It reads this text file called "EmployeeData.TXT":
S Washington,George 000001 125000
H MacDonald,Ronald 386218 7.80 true 40
H Walton,Samuel 268517 8.21 false
H Thomas,David 131313 9.45 true 38
H Sanders,HarlandDavid 277651 8.72 false
S Baron,James 368535 310236
:sign. But you don't have it in your line. And the array has only 1 element. Not two.array[1]is the second element of the array. It compiles fine, but in the runtime it breaks.