两个字符串之间的相等

时间:2013-06-07 20:46:37

标签: java string

我有一个包含这种结构的文本文件:

CRIM:Continuius
ZN:Continuius
INDUS:Continuius
CHAS:Categorical
NOX:Continuius   

我将它插入二维数组:

BufferedReader description = new BufferedReader(new FileReader(fullpath2));
        String[][] desc;
        desc = new String[5][2];

        String[] temp_desc;
        String delims_desc = ":";
        String[] tempString;

        for (int k1 = 0; k1 < 5; k1++) {
            String line1 = description.readLine();
            temp_desc = line1.split(delims_desc);
            desc[k1][0] = temp_desc[0];
            desc[k1][1] = temp_desc[1];
        }

然后尝试确定哪个属性是分类的:

        String categ = "Categorical";
        for (int i=0; i<5; i++){
            String temp1 = String.valueOf(desc[i][1]);
            if ("Categorical".equals(desc[i][1])){
                System.out.println("The "+k1+ " th is categorical.");
}
}

为什么不返回true,虽然其中一个属性是分类的?

1 个答案:

答案 0 :(得分:3)

查看您发布的输入(在edit perspective中),我看到文本文件的几乎每一行都有很多尾随空格。如果您更换

,您的问题将会消失
desc[k1][1] = temp_desc[1];

desc[k1][1] = temp_desc[1].trim();

您甚至可以将代码缩短为

for (int k1 = 0; k1 < 5; k1++) {
    String line1 = description.readLine().trim();
    desc[k1] = line1.split(delims_desc);
}

澄清:

您正在尝试比较

"Categorical" // 11 characters

"Categorical        " // more than 11 characters

那些不是平等的字符串。