Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to put the second half (after a whitespace tab) of a string into a n

ID: 3559759 • Letter: I

Question

I am trying to put the second half (after a whitespace tab) of a string into a new string array. However, I keep getting java.lang.ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException: 1 and I can't figure out why or which array it's talking about. Here is my code, HELP.

36    String aLine;
37    ArrayList result= new ArrayList(10000);
38    boolean hasAgg;
39    int numOfLines = 0;
40       
41    while ((aLine = bf.readLine()) != null) {
42         numOfLines++;
43    }

44

45    fr = new FileReader(filePath);
46    bf = new BufferedReader(fr);
47    int i;
48    String[] data = new String[numOfLines];

49

50    for (i=0 ; i < numOfLines ; i++) {

51          data[i] = bf.readLine();
52          hasAgg = data[i].contains("$Y");
53          if (hasAgg == true) {
54              String[] parts;
55           parts = data[i].split("/s");
56           result.add(parts[1]);

57          }
58     }

Explanation / Answer

You are getting an error at line 56

On line 55, you have parts = data[i].split("/s");
You want to split on whitespace.
This doesn't work.The splitting doesn't take place.
Because of this, parts is an array of size 1.
When you access parts[1](2nd element), you get an error because parts has only 1 element

Solution

Modify line 55 to parts = data[i].split("\s");

Have a look here for the sample code that works with my solution
http://ideone.com/7HjCOl

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote