if語句不讀取數組中的字符串 - tokens [i],因此if之後的條件不會執行。字符串「hot」不能被讀取。爲什麼?請幫忙!謝謝。如何比較java中的兩個數組字符串?
public static void main(String[] args){
String str = "Wow! It's getting realy hot in here." ;
String delims = "[ .,?!/><;:'_!`~/$/@/#/%/&/|/[/]/{/}/)/(]+";
String[] tokens = str.split(delims);
for (int i = 0; i < tokens.length; i++){
if (tokens[i] == "hot"){
System.out.println("Found!");
break;
}
else {
System.out.println("Not found!");
}
}
}
將字符串與'equals'比較,而不是'=='。 –
與[此帖子]類似(http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)。 – Frithjof
Nooooooooooo .. please nooooooooo:___( – Maroun