gpt4 book ai didi

java - 将 .csv 字符串值转换为 double

转载 作者:行者123 更新时间:2023-11-29 03:23:23 25 4
gpt4 key购买 nike

我在从 .csv 文件中读取字符串数值并将它们转换为 double 值时遇到困难。这是代码:

String filename = "/home/mahmoud/Desktop/CSV/remake1.csv";

BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
int val;

try {

br = new BufferedReader(new FileReader(filename));
while ((line = br.readLine()) != null) {

// use comma as separator
String[] country = line.split(cvsSplitBy);

val = Integer.parseInt(country[4]);

System.out.println(val);

}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

System.out.println("Done");
}

当我编译程序时出现以下错误“线程“主”java.lang.NumberFormatException 中的异常:对于输入字符串:“6.55”

这是 .csv 文件的内容:

2012-12-03,5.63,6.57,5.32,6.55,5816300,6.55

2012-11-01,4.66,5.68,4.37,5.59,5752000,5.59

2012-10-01,5.27,5.40,4.42,4.67,5958000,4.67

2012-09-04,5.91,6.56,5.21,5.26,5930600,5.26

关于如何解决该问题的任何建议?谢谢你

最佳答案

数字格式显示不是整数,而是 double

所以你应该将val声明为double:

double val;

然后使用下面一行来解析:

val = Double.parseDouble(country[4]);

关于java - 将 .csv 字符串值转换为 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22391699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com