【发布时间】:2017-01-10 13:40:07
【问题描述】:
我是 Java 新手,我正在使用打开的 CSV 读取 csv 文件,我的代码如下:
import java.io.FileReader;
import java.util.Arrays;
import au.com.bytecode.opencsv.CSVReader;
public class ParseCSVLineByLine
{
double arr []=new arr[10];
public static void main(String[] args) throws Exception
{
//Build reader instance
//Read data.csv
//Default seperator is comma
//Default quote character is double quote
//Start reading from line number 2 (line numbers start from zero)
CSVReader reader = new CSVReader(new FileReader("data.csv"), ',' , '"' , 1);
//Read CSV line by line and use the string array as you want
String[] nextLine;
int i=0;
while ((nextLine = reader.readNext()) != null) {
if (nextLine != null && i<10) {
//Verifying the read data here
arr[i]=Double.parseDouble(Arrays.toString(nextLine).toString());
System.out.println(arr[i]);
}
i++;
}
}
}
但这不起作用。但是当我只打印时
Arrays.toString(nextLine).toString()
打印出来
[1]
[2]
[3]
.
.
.
.
[10]
我认为转换有问题。感谢任何帮助。
【问题讨论】:
-
csv 文件中输入了什么?
-
Arrays.toString(nextLine)很可能会导致无法解析为双精度的内容,因为即使只有一个元素,您也会得到您发布的字符串,即"[1]"。为什么不只是Double.parseDouble(nextLine[index])而index可能是0? -
感谢您的接受,很高兴我的回答对您有帮助