我试图用扫描仪创建一个项目来读取文本文件,按行数分隔,并计算每行中的单词数。到目前为止,这是我的代码:
public void getWordsPerLine(){
try {
File file = new File("report.txt");
Scanner scanner = new Scanner(file);
int count = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
count++;
if (count <= 9){
System.out.println("");
System.out.println("Line Number: " + count);
System.out.println(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}