txt文件,我有一個包含這一個txt文件:的邏輯錯誤,而試圖掃描特定的詞
Mary had a little lamb little lamb
little lamb. Mary had a little lamb.
That's all? Did the lamb follow Mary
wherever Mary went?
我嘗試寫一些代碼,掃描單詞「瑪麗」 txt文件,統計它出現的次數,然後打印該數字。
import java.util.Scanner;
import java.io.*;
class Test {
public static void main(String[] args) {
int count = 0;
Scanner reader = new Scanner("test.txt");
while (reader.hasNextLine()) {
String nextToken = reader.next();
if (nextToken.equals("Mary"))
count++;
}
System.out.println("The word 'Mary' was found on " + count + " lines.");
}
}
雖然代碼編譯,我不斷收到不正確的輸入,與系統打印"The word 'Mary' was found on 0 lines."
任何想法是怎麼回事?
'contains()'而不是'equals()' –