gpt4 book ai didi

java - 为什么我的 hasNextLine() while 循环只工作一次?

转载 作者:行者123 更新时间:2023-12-01 13:08:40 25 4
gpt4 key购买 nike

我已经为此工作了几个小时,但自己或在线都找不到问题。我不知道为什么第一个循环确实有效,而第二个循环没有继续经过第一次迭代。

File adjacencies = new File(adjacenciesFile);
Scanner adjacenciesScanner = new Scanner(adjacencies);
//idk why this works and the while loop after doesn't
/*
while(adjacenciesScanner.hasNextLine()){
System.out.println(adjacenciesScanner.nextLine());
}
*/

while(adjacenciesScanner.hasNextLine()){
ArrayList<Country> adjacentCountries = new ArrayList<Country>();
String[] adjacenciesNames = adjacenciesScanner.nextLine().split(",");

for(String countryName : adjacenciesNames){
adjacentCountries.add(this.countries.get(countryName));
}
System.out.println(adjacentCountries);
adjacentCountries.remove(0);
this.countries.get(adjacenciesNames[0]).addAdjacencies(adjacentCountries);
}

这是我正在读取的文件:

Alaska,Alberta,Northwest Territory,Kamchatka
Alberta,Alaska,Northwest Territory,Ontario,Western United States
Central America,Western United States,Venezuela
Eastern United States,Western United States,Ontario,Quebec
Greenland,Northwest Territory,Ontario,Quebec,Iceland
Northwest Territory,Alaska,Greenland,Ontario,Alberta

没有异常(exception),只是不会运行多次

最佳答案

如果您设置断点并在 Debug模式下运行它,您应该确切地看到它退出循环的原因。对我来说看起来不错,如果有的话,你可能会在 split() 上遇到异常。

关于java - 为什么我的 hasNextLine() while 循环只工作一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23066236/

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