0

我对 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");
    }
}
4

1 回答 1

5

这是调试此类问题的方法。使用看起来相等但不相等的两个字符串调用以下方法:

public void dumpString(String s) {
    System.out.println("Length: " + s.length()); // check their length
    System.out.println("Value: >" + s + "<"); // check trailing and leading spaces
    System.out.println("numeric value of characters:"); // check which char differs
    for (int i = 0; i < s.length(); i++) {
        System.out.print((int) s.charAt(i));
        System.out.print(' ');
    }
}

使用调试器可以让您访问相同的信息。

于 2013-08-21T13:55:36.597 回答