I have a CSV file that contains a bunch of coordinates. In the file, the coordinate pairs are split up by commas. I have already split the pairs up and placed them into a ArrayList of strings. Now, I am trying to split each pair up into their respective longitude and latitude ArrayLists. So essentially, I am trying to use the .split() function twice (splitting the coordinates into pairs, the splitting the pairs into longitude and latitude values) and I am struggling to figure out how to use split the second time. I am not sure exactly what I am doing wrong or what the correct syntax is to accomplish this. I am working in Java. Here is what I have tried:
Path countyCoords = Paths.get("countyCoords.txt");
File coordsData = countyCoords.toFile();
FileReader frCoords = null;
BufferedReader brCoords = null;
ArrayList<String> coordsList1 = new ArrayList<>();
ArrayList<String> longitude = new ArrayList<>();
ArrayList<String> latitude = new ArrayList<>();
try {
frCoords = new FileReader(coordsData);
brCoords = new BufferedReader(frCoords);
String lineReader = brCoords.readLine();
while (lineReader != null) {
System.out.println("Coords: "+lineReader);
String[] threeLine = lineReader.split(",");
String[] values = threeLine.split(" ");
coordsList1.add(threeLine);
String lon = values[0];
String lat = values[1];
longitude.add(lon);
latitude.add(lat);
lineReader = brCoords.readLine();
}
} catch (IOException ex) {
System.err.println("Error: "+ex.getMessage());
} finally {
try {
if (brCoords != null) {
brCoords.close();
}
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
}
Assuming your data in the format
1 2, 3 4, 5 6The following snippet can be used to process a line from the data file. I omit exception handling, resources closing.