0

I am getting an error when trying to use a JFileChooser to scan a text file add it to an array and parse one of the strings to a double and two to integers. Does it have to do with the fact that the addEmployee method adds the six parameters to an arrayList? Here is the code...

else if (e.getSource()==readButton){
            JFileChooser fileChooser = new JFileChooser("src");
        if  (fileChooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
        {
            empFile=fileChooser.getSelectedFile();
        }
            Scanner scan = new Scanner("empFile");
            while(scan.hasNext()){
                String[] rowData = scan.next().split(":");
                if(rowData.length == 5){
                    rowData[4] = null;
                    fName = rowData[0];
                    lName = rowData[1];
                    position2 = rowData[2];
                    firstParam = Double.parseDouble(rowData[3]);
                    secondParam = Integer.parseInt(rowData[4]);
                    empNum = Integer.parseInt(rowData[5]);
                }
                else{
                fName = rowData[0];
                lName = rowData[1];
                position2 = rowData[2];
                firstParam = Double.parseDouble(rowData[3]);
                secondParam = Integer.parseInt(rowData[4]);
                empNum = Integer.parseInt(rowData[5]);
                }
                if (position2.equals("Manager")){
                    c.addEmployee(fName, lName, position2, firstParam, 0, empNum);
                }
                else if(position2.equals("Sales")){
                    c.addEmployee(fName, lName, position2, firstParam, 0, empNum);
                }
                else{
                    c.addEmployee(fName, lName, position2, firstParam, secondParam, empNum);
                }
            }

        }

John:Smith:Manufacturing:6.75:120:444

Betty:White:Manager:1200.00:111

Stan:Slimy:Sales:10000.00:332

Betty:Boop:Design:12.50:50:244

5
  • post the stacktrace/error here Commented Apr 26, 2014 at 0:51
  • also the the inside of the file you are parsing Commented Apr 26, 2014 at 0:51
  • Sorry ,what is the stacktrace/error? And I'm not parsing a file just the [3],[4], and [5] of the array. Commented Apr 26, 2014 at 0:53
  • the error the java gave you copy paste it here so we can examine it Commented Apr 26, 2014 at 0:54
  • Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 at GUI$aListener.actionPerformed(GUI.java:329) here is what the error is and line 329 is lName = rowData[1]; Commented Apr 26, 2014 at 0:55

2 Answers 2

1

You are trying to fetch empNum = Integer.parseInt(rowData[5]);

row data only having size 5 that means index 0-4, Thats why ArrayIndexOutOfBoundsException is getting

So Initialize String[] rowData = new String[6];

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

7 Comments

So I have edited the code to how it looks above but I am getting the same error may it have to do with how the textFile is?
you have to initialize the rowData as String[] rowData = new String[6]
Ok so I added the text for the textfile in my post above. Your answer seems right but didn't do the trick.
I did that and it didn't do it.
Now also you are getting the same exception. Can you post the exception string
|
0
  String[] rowData = new String[5]; // edited out of original post
  rowData = scan.next().split(":");

The first statement allocates an array of 5 Strings and sets them all to null. The second statement just throws away the array you just allocated. The result of split will return an array of however many items it finds, and then you assign rowData, which is a reference to an array, to a reference to the new array. The old one gets garbage collected. So there's no guarantee that rowData will have 5 elements after this assignment.

You'll have to decide what you want to do if the split doesn't return enough array elements. You could use something like Arrays.copyOf that could put the split result into some of the rowData elements while leaving the rest alone, but then the unassigned elements will still be null, and you'll just get a NullPointerException a few lines later. So you have some design decisions to make here.

More: You may want to consider using the Scanner method nextLine() instead of next(). next() will return just one token, which means it will stop at a space character, which means you will have problems if someone is named "Mary Beth" or something like that.

4 Comments

Oh actually I see what your saying how can I make it that it puts a null when there is nothing.
How do your if, if else, and else check for the wrong number of fields? Anyway, you look at rowData[5] before your if. If there is some position that won't have as many fields, you would need to make sure you don't try to access a nonexistent field until after you check the position.
Ok so I have edited the code as you can see above but I am getting the same error on the lName = rowData[1]; of the else statement. How can I make the code so it reads in the code and checks for the length.
@user3530205 I don't know why you're getting the error; perhaps you need to print rowData.length and see what it is, and maybe also print the string you're splitting (by saving scan.next() in a variable). You already know how to check rowData.length. But if it's 0 or 1, you need to figure out why, and if it's an error in your input file, you probably should do a "sanity check" before doing anything else (if (rowData.length < 5) then print an error message else { do the rest of your logic }, or something.

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.