我有一个包含整数的CSV文件:
的数据
我把它转换成一个String数组:
try (BufferedReader br = new BufferedReader(new FileReader("x.csv"))) {
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split("\n");
x_values.add(Arrays.asList(values));
}
} catch (IOException e) {
e.printStackTrace();
}
字符串
输出如下:output
但我想用整数的值来求它们的平均值我怎么能做到呢?
2条答案
按热度按时间qzlgjiam1#
您正在将行解析为
String
值,而不是Integers
。从你的图像来看,你的整数是指数符号,这意味着你必须首先将它们解析为double,然后将它们转换为整数:字符串
你应该确保你的csv文件只包含整数,否则使用
List<Double> x_values
代替,跳过int转换。vtwuwzda2#
一个稍微现代和简洁的答案
字符串