gpt4 book ai didi

java - 将字符串文件添加到数组列表中

转载 作者:行者123 更新时间:2023-12-02 05:55:44 27 4
gpt4 key购买 nike

我正在尝试将此文件添加到数组列表中,稍后我将其与另一个数组列表进行比较。到目前为止我已经有了这个,但它给了我一个编译错误。它说它找不到符号 .hasNext 和 。读行。

ArrayList<String> america = new ArrayList<String>();
while((infile2.hasNext()))
{
america.add(infile2.nextLine());
}

有人可以帮我弄清楚如何修复这些错误吗?

最佳答案

Yes it is a bufferedReader

BufferedReader 没有 hasNext 方法,因此您可以使用 readLine

String line;
while((line = infile2.readLine()) != null) {
americaList.add(line);
}
...

或者如果您可以使用文件

List<String> americaList = 
Files.readAllLines(Paths.get("list.txt"), StandardCharsets.UTF_8);

关于java - 将字符串文件添加到数组列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23097980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com