I am having some trouble converting a CSV file into a 2D array in java. I might be going the longest way around this but I cannot seem to figure our why I am getting an error. Each row and column is supposed to have 25 elements each. Here is my code:
BufferedReader CSVFile = new BufferedReader(new FileReader(fileName));
String dataRow = CSVFile.readLine();
// Read first line.
// The while checks to see if the data is null. If
// it is, we've hit the end of the file. If not,
// process the data.
while (dataRow != null) {
dataRow.split(",");
list.add(dataRow);
dataRow = CSVFile.readLine();
// Read next line of data.
}
// Close the file once all data has been read.
CSVFile.close();
String[] tokens = null;
Object[] test = list.toArray();
String[] stringArray = Arrays.copyOf(test, test.length, String[].class); //copies the object array into a String array
//splits the elements of the array up and stores them into token array
for (int a = 0; a < test.length; a++) {
String temp = stringArray[a];
tokens = temp.split(",");
}
//converts these String tokens into ints
int intarray[] = new int[tokens.length];
for (int i = 0; i < tokens.length; i++) {
intarray[i] = Integer.parseInt(tokens[i]);
}
//attempts to create a 2d array out of a single dimension array
int array2d[][] = new int[10][3];
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
array2d[i][j] = intarray[(j * 25) + i];
}
}
I believe that the error is when the ArrayList gets copied to the first String array but I can't be sure. The file has 25 columns and 25 rows. The error I keep getting are that the array is out of bounds at index 25. Any input would be greatly appreciated. Thanks!