1

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
7
  • virtually all this code is unnecessary. Read up on how to make minimal, complete, verifiable examples. Commented Nov 5, 2014 at 21:04
  • Normally people post not enough code... Commented Nov 5, 2014 at 21:08
  • The problem is the parsing of the line. You use : 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. Commented Nov 5, 2014 at 21:08
  • It's a skill gained through experience as to how much is enough and how much is too much. It's always good to read the tour and the help center sections to get recommendations. Commented Nov 5, 2014 at 21:09
  • 1
    That's an awful lot of writing for something that 5 minutes of debugging would have figured out. Commented Nov 5, 2014 at 21:53

2 Answers 2

3

You're splitting on ":", and your text file:

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

does not have any such character. So each array will have size 1.

It makes no sense to split on a non-existing character, which makes me very curious why you would choose to use ":". Myself, I'd split on white space, "\\s+"

As an aside, this problem is very amenable to debugging either with a debugger or with some printlns:

while(infile.hasNext()){
    String line = infile.nextLine();
    System.out.println("Unsplit String: " + line);
    String array[] = line.split(":");
    System.out.println("Split String: " + java.util.Arrays.toString(array));

Either way, in the future when you have similar problems, find out just what your variables are holding at the time and place of the error, and that often will lead you to the origin of the problem and thereby to its solution.

Sign up to request clarification or add additional context in comments.

Comments

1

It is breaking on these lines:

    String line = infile.nextLine();
    String array[] = line.split(":");
    String name = array[1];
    String id = array[2];

You are trying to assign the value of array[1] to name... But the array doesn't contain enough elements.

So say you have line = "hey" and you call split on line, your array will only be of length 1, since there is no ":" character to split on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.