抛出NoSuchElementException的Java Arraylist程序

Cur*_*111 1 java algorithm data-structures

我的程序应该接受任何数据类型的文件输入并显示它.但是在读完第7个元素后,我收到错误"NoSuchElementException"

这是我的代码:

这是我的代码

Joh*_*son 9

在while循环中,你连续做了两个"in.next()"而没有检查"in.hasNext()"

您应该将in.next()存储在变量中,然后将该变量添加到ArrayType和LinkType.

while(in.hasNext()) {
    Object o = in.next();
    ArrayType.add(o);
    LinkType.add(o);
}
Run Code Online (Sandbox Code Playgroud)

根据您的评论,如果您只想将其打印出来以查看其他所有内容都正常工作,请使用以下命令:

public static void main(String[] args) throws IOException {
    List<String> lines = Files.readAllLines(Paths.get(("input.txt")));
    for (String line : lines) {
        System.out.println(line);
    }
}
Run Code Online (Sandbox Code Playgroud)