当我尝试将字符串数组转换为double数组时,我不断收到“floatingdecimal.readjavaformatstring”错误。请注意,字符串数组看起来像这样:(但它会持续更长时间)
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
我的转换看起来像这样:(其中str1是一个String [])
double[] array1 = new double[str1.length];
for (int i = 0; i < array1.length; i++)
{
array1[i] = Double.parseDouble(str1[i]);
}
我很确定转换是正确的,但我的字符串中有些东西是不对的,例如额外的逗号或其他内容。你有什么建议可以修理我的字符串吗?或者你认为我应该以不同的方式进行转换?
非常感谢,
KJM
答案 0 :(得分: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]);
}
}
试试这个,它对我来说很好。
答案 1 :(得分:0)
我相信您没有使用String[] str1 = givenString.split(",");