I'm trying to read from this CSV file that looks like
"1.23","2.45","3.25"
"4","5","6"
"7","8","9"
"10","11","12"
Now the problem is that while Java can find my file, line split isn't returning what I'm expecting.
Here is my attempt at reading from the csv file.
File root = new File(Thread.currentThread().getContextClassLoader().getResource("").toURI());
File resource = new File(root, "coords.csv");
float[] x = new float[5000];
float[] y = new float[5000];
float[] z = new float[5000];
int coordIndex = 0;
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
/* 1, 2, 3,
* 3, 5, 6,
*/
try{
System.out.println(coordIndex);
br = new BufferedReader(new FileReader(resource));
String[] coordinates = line.split(cvsSplitBy);
/*I get an error from accessing an array out of bounds */
System.out.println(coordinates[0]);
System.out.println(coordinates[1]);
System.out.println(coordinates[2]);
x[coordIndex] = Float.parseFloat(coordinates[0]);
y[coordIndex] = Float.parseFloat(coordinates[1]);
z[coordIndex] = Float.parseFloat(coordinates[2]);
coordIndex++;
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is my output error:
IOException: java.lang.ArrayIndexOutOfBoundsException: 1
Any ideas??
") first withreplaceAll()and then split by comma (,). As of know your are leaving both leading and trailing quotes.