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);
}
}
Two errors come from this: the first is a "cannot find symbol" error at str1.replaceAll. The second is a "method parseDouble" error at "Double.parseDouble". The issue there is I need a String instead of a String[].
Any ideas on how to convert my String[] to a double[] ?
Thanks,
kjm
You need to split each String in list1 on "," and attempt to parse each String that gets split out:
ArrayList<Double[]> results = Lists.newArrayList();
for( String s : list1 ) {
String[] splitStrings = s.split(",");
Double[] doublesForCurrentString = new Double[splitStrings.length];
for(int i=0; i<splitStrings.length; i++){
try {
doublesForCurrentString[i] = Double.valueOf(splitStrings[i]);
} catch( NumberFormatException ex ) {
// No action.
}
}
results.add(doublesForCurrentString);
}
Double[][] doubleArray = (Double[][])results.toArray();
Crucial points:
- EDIT: As @Tim Herold points out, you're probably better of performance-wise avoiding attempting to parse content you know to be non-numeric. In this case, I'd still split first and then just put in code that prevents you from attempting to parseDouble() on the first split String in each line, rather than trying to do String replacement before the split; that will be faster (and if we're not concerned about performance, then try/catch is perfectly fine and more readable). ORIGINAL: You need a try/catch when you try to parse the doubles, in case there's any invalid input. Bonus: you don't need to remove the non-numeric text now, you can just let this try/catch handle it.
- Your strings have two doubles in each of them. You're not going to be able to just strip the text at the beginning and then parse the rest, because it's not going to be a valid double.
- ArrayLists are generally easier to use; I'd opt for returning
ArrayList<Double>
(or ArrayList<ArrayList<Double>>
) over Double[]
or Double[][]
any day of the week. There are certainly situations where I'd do differently, but yours doesn't sound like one of them to me.
Loop throug the Array:
foreach String[]
double[counter] = parseToDouble(String[counter])
EDIT:
Java:
String[] str1 = list1.toArray(str1);
double[] dou1 = new double[str1.length]
for(int counter = 0; counter < str1.length;counter++)
dou1[counter] = Double.parseDouble(str1[counter].replaceAll("RSP_\\d+",""));
Use this
String[] str1 = new String[list1.size()];
str1 = list1.toArray(str1);
double[] doubleArray = new double[str1.length]
int i=0;
for(String s:str1){
doubleArray[i] = Double.valueOf(s.trim());
i++;
}
This will do the work.
double[] doubles = new double[array1.length];
for(int i=0; i<array1.length; i++){
doubles[i] = Double.valueOf(array1[i])
}
Thanks SmartLemon for the edit, you could do this too instead of using a String[]
:
double[] doubles = new double[list1.size()];
for(int i=0; i<list1.size(); i++){
doubles[i] = Double.valueOf(list1.get(i).replace("RSP_\\d+",""));
}
EDIT: So yes, you need a delimiter:
double[] doubles = new double[array1.length*2]; //multiply by 2 because you really have 8 numbers not 4
String [] array2 = null;
for(String string: array1) {
array2 = string.split(",");
for(int i=0; i<array2.length; i++){
doubles[i] = Double.valueOf(array2[i]);
}
}
this should do the work for sure.
I would do this:
- make array
- convert to string
then this code
for (String s: list1) {
boolean obtained = false;
double tempD;
try {
tempD = Double.parseDouble(s);
obtained = true;
} catch (Exception e) {
e.printStackTrace();
}
if (obtained) {
list2.add(tempD);
}
}