我对 Java 中的 .equals 函数有疑问。我很确定问题出在两个字符串不一样,即使它们从肉眼来看(不同的格式)。但我似乎无法调试它,所以想知道你们能帮我吗?
基本上,这只是一小段代码,将用于读取 .CSV 并解析前 1 - 3 个单词减去任何数字。我知道它可以更好地编码,但它只会运行一次。它工作正常,但假设删除当前不起作用的重复项。如果你们能对我如何解决这个问题有所了解,将不胜感激。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadCSVTest {
public static void main(String[] args) {
ReadCSVTest obj = new ReadCSVTest();
obj.run();
}
public void run() {
String csvFile = "C:/Users/mrx/Documents/Standard Software.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
int count = 0;
String duplicate = "";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] product = line.split(cvsSplitBy);
String app = product[0];
app = app.toUpperCase();
app = app.replaceAll("[^A-Z -]", "");
app = app.trim();
if(!app.equals(duplicate)) {
if(app.contains(" ")){
String words[] = app.split(" ");
String firstTwo = words[0] + " " + words[1];
if(firstTwo.contains("MICROSOFT")){
if(words.length > 2){
String firstThree = words[0] + " " + words[1] + " " + words[2];
duplicate = firstThree.trim();
System.out.println("\"" + duplicate + "\", ");
count++;
}
else{
duplicate = firstTwo.trim();
System.out.println("\"" + duplicate + "\", ");
count++;
}
}
else{
duplicate = firstTwo.trim();
System.out.println("\"" + duplicate + "\", ");
count++;
}
}
else{
duplicate = app.trim();
System.out.println("\"" + duplicate + "\", ");
count++;
}
}
}
}
catch (FileNotFoundException e){ e.printStackTrace();}
catch (IOException e){ e.printStackTrace();}
finally{
if (br != null) {
try{ br.close(); }
catch (IOException e) { e.printStackTrace();}
}
}
System.out.println("Done");
System.out.println(count + " Programs");
}
}