Java中的.equals()函数问题

时间:2013-08-21 13:49:43

标签: java string-comparison

我遇到了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");
    }
}

1 个答案:

答案 0 :(得分: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(' ');
    }
}

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