扫描仪不再读取行 - 抛出 NoSuchElementException

Scanner is no longer reading lines - throwing NoSuchElementException

我的扫描仪工作正常,读取线条非常漂亮,现在它不再工作并继续抛出 NoSuchElementException: No Line Found 错误。

我的代码:

        try {
            File file = new File("Word200D16.txt");
            Scanner scanner = new Scanner(file);
            while(scanner.hasNextLine()){
                for(int i = 0; i < 200; i++){
                    String line = scanner.nextLine();
                    elementsToAdd[i] = line;
                }
            }

        } catch(Exception er) {
            System.out.println("Error: " + er);
        }

我忽略的这段代码有什么明显错误吗?我希望每一行都保存到我的字符串数组 elementsToAdd.

Is there anything obviously wrong with this code that I am overlooking?

是的。您正在检查 hasNextLine 一次,然后在没有检查的情况下循环调用 nextLine 200 次。