gpt4 book ai didi

java - 即使 CSV 行中没有值,OpenCSV 也会返回一个字符串

转载 作者:行者123 更新时间:2023-11-30 11:34:10 25 4
gpt4 key购买 nike

我正在编写一个程序来检查前两行(不包括标题)是否包含任何数据。如果没有,则忽略该文件,如果前两行中的任何一行包含数据,则处理该文件。我正在使用 OpenCSV将标题、第一行和第二行检索到 3 个不同的数组中,然后根据我的要求检查它们。我的问题是,即使前两行为空,reader 也会返回类似 [Ljava.lang.String;@13f17c9e 的内容作为第一行和/或第二行(取决于我的测试文件)。

为什么除了 null 之外,它根本不返回任何东西?

最佳答案

我现在不在我的电脑前,所以请原谅任何错误~ OpenCSV API Javadocs 相当简短,但似乎没有太多内容。读取一行应该将内容解析为一个字符串数组。一个空行应该导致一个空字符串数组,如果您尝试打印它,它会给出类似 [Ljava.lang.String;@13f17c9e 的内容...

我假设以下示例文件:

1 |
2 |
3 | "The above lines are empty", 12345, "foo"

如果你执行 myCSVReader.readAll() 会产生以下内容

// List<String[]> result = myCSVReader.readAll();
0 : []
1 : []
2 : ["The above lines are empty","12345","foo"]

要执行您在问题中描述的内容,请测试长度而不是某种空值检查或字符串比较。

List<String> lines = myCSVReader.readAll();

// lets print the output of the first three lines
for (int i=0, i<3, i++) {
String[] lineTokens = lines.get(i);
System.out.println("line:" + (i+1) + "\tlength:" + lineTokens.length);
// print each of the tokens
for (String token : lineTokens) {
System.out.println("\ttoken: " + token);
}
}

// only process the file if lines two or three aren't empty
if (lineTokens.get(1).length > 0 || lineTokens.get(2).length > 0) {
System.out.println("Process this file!");
processFile(lineTokens);
}
else {
System.out.println("Skipping...!");
}

// EXPECTED OUTPUT:
// line:1 length:0
// line:2 length:0
// line:3 length:3
// token: The above lines are empty
// token: 12345
// token: foo
// Process this file!

关于java - 即使 CSV 行中没有值,OpenCSV 也会返回一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15799354/

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