-2

When I try to convert a string array to a double array, I keep getting a "floatingdecimal.readjavaformatstring" error. Note that the string array looks something like this: (but it goes on for much longer)

7.641844673,7.643565703,7.319638605,7.42366145,7.419292812,7.388869123,7.53670762,7.749329445,7.625242329,7.322164604,7.315094508,7.403445746,7.890969983,7.544904537,7.677043042,7.477693567,7.527992118,7.415580204,7.417685294,7.393078839

And my conversion looks like this: (where str1 is a String[] )

double[] array1 = new double[str1.length];
for (int i = 0; i < array1.length; i++) 
{
array1[i] = Double.parseDouble(str1[i]);
}

I'm pretty sure that the conversion is correct, but there's something in my string that's amiss, such as an extra comma or something. Do you have any suggestions as to how I can fix my string? Or do you think I should do my conversion differently?

Thanks a lot,

KJM

4

2 回答 2

0
public static void main(String[] args) {
        String str1 = "7.641844673,7.643565703,7.319638605,7.42366145,7.419292812,7.388869123,7.53670762,7.749329445,7.625242329,7.322164604,7.315094508,7.403445746,7.890969983,7.544904537,7.677043042,7.477693567,7.527992118,7.415580204,7.417685294,7.393078839";
        String[] split = str1.split(",");
        double[] doublearray = new double[split.length];
        for (int i = 0; i < split.length; i++) {
            doublearray[i] = Double.parseDouble(split[i]);

        }
        for (int i = 0; i < doublearray.length; i++) {
            System.err.println(doublearray[i]);
        }
    }

试试这个,对我来说效果很好。

于 2013-08-08T23:51:13.127 回答
0

我相信你没有使用String[] str1 = givenString.split(",");

于 2013-08-09T00:00:12.987 回答