Thursday, 8 August 2013

Convert String[] with text to double[]

Convert String[] with text to double[]

I have an Arraylist that I want to convert to a double[] array. I first
convert the Arraylist to a String []. I then try to convert the String[]
to a double[] but fail in the process. Here's the issue: the string
contains some text, as well as some numbers with decimals. I want to
convert only the numbers and decimals to a double[] array and simply
delete the text. However, I only know how to delete the text with a
String, not a String[]. Please take a look:
import java.util.ArrayList;
import java.util.List;
import jsc.independentsamples.SmirnovTest;
public class example{
public static void main(String[] arg) throws Exception {
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
list1.add("RSP0001,1.11,1.22");
list1.add("RSP0002,2.11,2.22");
list1.add("RSP0003,3.11,3.22");
list1.add("RSP0004,4.11,4.22");
String[] str1 = new String[list1.size()];
str1 = list1.toArray(str1);
str1.replaceAll("RSP_\\d+","");
double array1 = Double.parseDouble(str1);
System.out.println(array1);
}
}
Any ideas on how to convert my String[] to a double[] ?
Thanks,
kjm

No comments:

Post a Comment