java - 不明白在处理 3 中访问 CSV 文件是如何工作的。需要解释

标签 java csv processing

我之前使用文本编辑创建了一个名为 titanicLinearishedDataSet.csv 的 CSV 文件。我的目标是使用处理 3 访问此文件并检查列中的元素是否等于字符串值“Nil”。我没有收到结果,而 csv 文件包含“Nil”。

我已经合并了 CSV 文件的图像 (CSV_file_image.jpg) .

感谢您的帮助!

String [][] array;
void setup() {
    String [] lines = loadStrings("titanicLinearisedDataSet.csv");

    array = new String[lines.length][3];
    int i = 0;

    for(String line: lines){
        String [] pieces = split(line,",");
        if(pieces[3] == "Nil"){
            println("It worked");
    }
}

最佳答案

您不应使用 == 来比较 String 值。您应该使用 equals() 函数来代替:

if(pieces[3].equals("Nil")){
  println("It worked");
}

来自the reference :

To compare the contents of two Strings, use the equals() method, as in if (a.equals(b)), instead of if (a == b). A String is an Object, so comparing them with the == operator only compares whether both Strings are stored in the same memory location. Using the equals() method will ensure that the actual contents are compared.

还要注意你的大括号。您发布的代码似乎缺少一个。

如果这不起作用,请尝试养成 debugging your code 的习惯。例如,尝试打印 piecespieces[3] 的值,以准确了解发生了什么。

关于java - 不明白在处理 3 中访问 CSV 文件是如何工作的。需要解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50143049/

相关文章:

processing - 如何将处理草图导出为视频?

java - 将处理 pde 导入 Eclipse

python - 从嵌套Python字典导出csv

json - 使用 R 的 Plumber - 创建 GET 端点来托管 CSV 格式的数据而不是 JSON

java - 如何在 Java 中搜索 XML 文件,然后输出 XML 文件名?

java - 为什么相同算法的运行时间差异如此之大?

python - 将 CSV 文件注释行保留在 pandas 中?

java - 代码颤动(线条有点跳跃,看起来很糟糕)

java - 如何从 Android map Activity 中的 url 获取 JSON 数据

java - Android 使用 jaudiotagger