.txt ファイル内の文字列の数をカウントする単純なスキャナーを作成しました。各文字列は nextLine にあります。20,000 を超える文字列があっても、297 という数字が表示されるたびに、カウントが間違っています。.txt ファイルは、私がコーディングした別のプログラムによって作成されたもので、Web サイトからリンクを取得し、FileWriter および BufferedWriter を使用してそれらを .txt ファイルに保存します。何が間違っている可能性がありますか?
public class Counter {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("/Users/myName/Desktop/test.txt"));
String string = scanner.next();
int count = 0;
while (scanner.hasNextLine()) {
string = scanner.next();
count++;
System.out.println(count);
}
}
}
編集: 文字列の例:
yahoo.com
google.com
etc.