-3

I am reading a txt file and trying to save it into a 2-dimensional(2D) array, but got the error,the .txt file is saved definitely as a 100*997 array splitted by \t. It says

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 924!

int i=0;

double[][] vector=new double[100][997];

String line;

while ((line=br.readLine()) != null) {
    line=line.trim();
    String[] words = line.split("\t");
    for (int j=0; j<997; j++) {

       vector[i][j] = Double.parseDouble(words[j]);
    }
    i++;
}

Why is it out of bounds at 924?

Thank you very much!

Chris
  • 4,450
  • 3
  • 38
  • 49

1 Answers1

-2

line=line.trim();

Probably, the line has only 924 words after trim();

This would throw an error on reference to words[924].

ravitimm
  • 114
  • 8
  • I checked data file, the last row has exact 997 words,all of them are doubles – Xiaofeng Liu Apr 04 '17 at 00:32
  • also at the beginning I didn't have line=line.trim(), same error – Xiaofeng Liu Apr 04 '17 at 00:37
  • Sorry! After I double checked the txt file, I found it IS NOT complete which means I didn't use .close() when I tried to write the file, so the data is not completely written into the file ended on 924, that is why I got the error! – Xiaofeng Liu Apr 17 '17 at 15:49