簡體   English   中英

兩個字符串之間的相等性

[英]equality between two strings

我有一個具有以下結構的文本文件:

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

查看您發布的輸入(在edit透視圖中 ),我發現幾乎在文本文件的每一行上都有很多尾隨空格。 如果更換,您的問題將消失

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

那些不是相等的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM