I am trying to read in a text file line by line. On each line there are two double numbers separated by a comma (representing x,y points). I would like to put them in two arrays of doubles (one containing the x-points and the other the y-points) in order that they are in the file. The arrays should be the same size as the number of points there are in the file (i.e. the number of lines in the file).
The files could have different numbers of points, so I do not want to declare a fixed size for all files. How do I go about doing this?
EDIT:
There is something wrong with the way I am doing this:
BufferedReader input;
ArrayList<Double> xpointArrayList = new ArrayList<Double>();
ArrayList<Double> ypointArrayList = new ArrayList<Double>();
try {
input = new BufferedReader(new FileReader(args[0]));
String line;
while ((line = input.readLine()) != null) {
line = input.readLine();
String[] splitLine = line.split(",");
double xValue = Double.parseDouble(splitLine[0]);
double yValue = Double.parseDouble(splitLine[1]);
xpointArrayList.add(xValue);
ypointArrayList.add(yValue);
}
input.close();
} catch (IOException e) {
} catch (NullPointerException npe) {
}
double[] xpoints = new double[xpointArrayList.size()];
for (int i = 0; i < xpoints.length; i++) {
xpoints[i] = xpointArrayList.get(i);
}
double[] ypoints = new double[ypointArrayList.size()];
for (int i = 0; i < ypoints.length; i++) {
ypoints[i] = ypointArrayList.get(i);
}
When I do the Array.toSring call on the xpoints and the ypoints array. It only has one number. For example in the file:
1,2 / 3,4 / 0,5
It only has 3.0 for the xpoints array and 4.0 for the ypoints array. Where is it going wrong?
How about just making a single
ArrayListof typePoint. AnArrayListwill grow to the size of the content, so it should be as simple as doing something like this (psuedocode)…The
Pointclass is designed as a container for 2doublevalues, used to represent any abstract point, which should suit your need perfectly. Refer to the Point documentation.If you really need to use 2 arrays, my recommendation is to use an
ArrayList<Double>…Refer to the ArrayList documentation for more information about this.
If you really need to use arrays, you can convert the
ArrayListto an array like this…