我之前使用文本编辑创建了一个名为 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 inif (a.equals(b))
, instead ofif (a == b)
. AString
is anObject
, so comparing them with the==
operator only compares whether both Strings are stored in the same memory location. Using theequals()
method will ensure that the actual contents are compared.
还要注意你的大括号。您发布的代码似乎缺少一个。
如果这不起作用,请尝试养成 debugging your code 的习惯。例如,尝试打印 pieces
和 pieces[3]
的值,以准确了解发生了什么。
关于java - 不明白在处理 3 中访问 CSV 文件是如何工作的。需要解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50143049/