![](/img/trans.png)
[英]Java Streams -> Extract value at a particular index from List<List<String>>
[英]Detecting a particular string value from a file, Java
我正在嘗試檢測外部txt文件中的特定字符串值。 在這種情況下,我試圖檢測%%。
public void read(){
while(x.hasNext()){
String a = x.next();
System.out.println(a);
if(a == "%%"){
System.out.println("Found the end");
}
else{
System.out.println("No end");
}
}
}
它正在正確讀取x,因為它會打印出文件中的單詞,但在每個單詞之后都顯示“ No end”,即使是“ %%”也是如此。 我確定我在某個地方犯了一些愚蠢的錯誤,但我只是找不到。 x看起來像這樣:蘋果橙%%葡萄。 任何幫助都將不勝感激。
您必須使用String::equals()
進行比較,但不能使用運算符==
。 ==
運算符將String的地址位置和字符串文字%%
進行比較。
嘗試
if(a.trim().equals("%%"))
{
System.out.println("Found the end");
}
那應該消除字符串末尾的多余空格,並確保良好的比較!
您應該使用equals
方法而不是==
來檢查參考的邏輯等式。
if(a.equals("%%")){
System.out.println("Found the end");
}
else{
System.out.println("No end");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.