Hi folks, I have a string that will be different each time but follow the form of -3.33,-46.53,37.39,26.55,97.11,68.46,-32.46,-5.89,-62.89,-7.9, and i want to remove each number and store as an double in an array. even pseudocode would be great i'm drawing a blank. Cheers
+4
A:
String[] doubles = myString.split(",");
then iterate over the doubles array and do Double.parseDouble();
Yishai
2009-09-13 16:16:43
A:
Maybe something like:
String[] sa = yourString.split(",");
Double[] da = new Double[sa.length];
int idx = o;
for(String s : sa) {
da[idx++] = Double.parseDouble(s);
}
JRL
2009-09-13 16:21:25
+1
A:
Alternative way, reads input from file and parses items as thy are read.
static java.util.ArrayList<Double> getData(String filename) throws FileNotFoundException {
java.util.ArrayList<Double> result = new java.util.ArrayList<Double>();
java.util.Scanner sc = new java.util.Scanner(new java.io.File(filename));
sc.useDelimiter(",");
while (sc.hasNext())
result.add(Double.parseDouble(sc.next()));
sc.close();
return result;
}
If you want, you can just store them in a array like:
Double data[] = null;
getData("input.dat").toArray(data);
Margus
2009-09-13 16:22:09