2013-04-22 87 views
28

我無法用下面的代碼來比較兩個字符串:字符串比較 - 安卓

我有一個名爲「性別」,這將有「男」或「女」作爲其值的字符串。

if(gender == "Male") 
    salutation ="Mr."; 
if(gender == "Female") 
    salutation ="Ms."; 

這並沒有工作,所以我嘗試了以下內容:

String g1="Male"; 
String g2="Female"; 
if(gender.equals(g1)) 
    salutation ="Mr."; 
if(gender.equals(g2)) 
    salutation ="Ms."; 

再次,它沒有工作。 有人可以告訴我如何使用if語句比較字符串值。

+1

你有答案嗎? – 2013-04-22 09:30:08

+3

可能的重複[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Blackbelt 2013-04-22 09:38:19

+5

可能的重複[如何比較在Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – SilentKiller 2017-05-08 12:21:26

回答

72

試試這個

if(gender.equals("Male")) 
salutation ="Mr."; 
if(gender.equals("Female")) 
salutation ="Ms."; 

中還刪除;(分號)的if語句

if(gender.equals(g1)); 

在Java中,最常見的錯誤滿足新人使用==比較字符串一個。你必須記住,==比較對象引用,而不是內容。

+3

如果你解釋爲什麼,我會+1這個:) – 2013-04-22 09:27:03

+0

看看你的仔細編碼並刪除;在兩個if語句結束時。 – 2013-04-22 09:33:20

+0

對不起,我沒有用;在我的實際代碼中的if stmt之後,在這裏輸入問題時出現錯誤! – 2013-04-22 09:34:20

3

試試這個

String g1="Male"; 
    String g2="Female"; 
    if(gender.equals(g1)) 
     salutation ="Mr."; 
    if(gender.equals(g2)) 
     salutation ="Ms."; 

你結束你的if語句if(gender.equals(g1)); < < ---這裏加 「;」

8

在Java中,你在上面幹什麼,我們不比較字符串... 這裏是字符串比較...

if (gender.equalsIgnoreCase("Male")) { 
     salutation = "Mr."; 
    } else if (gender.equalsIgnoreCase("Female")) { 
     salutation = "Ms."; 
    } 
0
if(gender.equals(g1)); <--- 
if(gender == "Female"); <--- 

你if.REMOVE之後分號。

2

這應該工作:

if(gender.equals("Male")){ 
salutation ="Mr."; 
} 
else if(gender.equals("Female")){ 
salutation ="Ms."; 
} 

記住,不要到後if語句中使用;

4

gender == "Male"實際上是比較的對象gender和不同的對象Male對象的引用。你必須使用.equals()方法來比較對象的值。

3
String g1="Male"; 
String g2="Female"; 
String salutation=""; 
String gender="Male"; 
if(gender.toLowerCase().trim().equals(g1.toLowerCase().trim())); 
    salutation ="Mr."; 
if(gender.toLowerCase().trim().equals(g2.toLowerCase().trim())); 
    salutation ="Ms."; 
3

試試這個。

 String g1 = "Male"; 
     String g2 = "Female"; 
     String gender = "Male"; 
     String salutation = ""; 
     if (gender.equalsIgnoreCase(g1)) 

      salutation = "Mr."; 
     else if (gender.equalsIgnoreCase(g2)) 

      salutation = "Ms."; 
     System.out.println("Welcome " + salutation); 

輸出:

Welcome Mr. 
6

我認爲上述答案是correct.Because ==是用於測試兩個字符串是否是同一個對象,而。equals()測試兩個字符串是否具有相同的值。

+0

這是更好的解釋爲什麼不使用== – Dexter 2014-11-26 11:04:34

+1

沒有「above」的答案,因爲順序因數而異的選票。請儘可能編輯並指定。 TKS – 2016-08-21 10:37:56

0

實際上,每個代碼在這裏運行良好,但是您的問題可能來自您的gender變量。在比較之前你有沒有嘗試做一個簡單的System.out.println(gender);

12

與int或其他數值變量不同的字符串在Java中與其他語言不同。

要比較Java中的字符串(android),它使用方法.compareTo();

所以代碼應該是這樣的:

if(gender.compareTo("Male")==0){ 
    salutation ="Mr."; 
} 
if(gender.compareTo("Female")==0){ 
    salutation ="Ms."; 
} 
-1

試試:

if (Objects.equals(gender, "Male")) { 
    salutation ="Mr."; 
} else if (Objects.equals(gender, "Female")) { 
    salutation ="Ms."; 
} 
0

可以使用,contentEquals()函數。它可能會幫助你..