我正在尝试读取文件并计算文件中的行数。每行都有一个电影标题。我注释掉了增加计数的行。如果我取消注释计数的递增,程序就会挂起并且永远不会打印计数。我究竟做错了什么?如果我只打印 while 循环中的每一行,它就可以正常工作。但如果我尝试增加计数则不会。谢谢。
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
public class GuessTheMovie {
public static void main(String[] args) throws Exception {
try {
File file = new File("movies.txt");
Scanner scanner = new Scanner(file);
// open movies file and count the number of titles in the file
int count = 0;
while (scanner.hasNextLine()) {
//count += 1;
System.out.println(scanner.nextLine());
}
System.out.println(count);
// create String array of movies such that the size is equal to the number of movies in file.
//String [] movies = []
} catch (Exception e) {
System.out.println("Could not find file.");
}
}
}
万千封印
手掌心
弑天下
肥皂起泡泡
相关分类