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

时间:2014-04-14 17:16:12

标签: java java.util.scanner

我一直在努力工作几个小时,无法自己或在线找到问题。我不知道为什么第一个循环确实有效,第二个循环没有超过第一个循环。

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

没有例外,它只是不会运行多次

1 个答案:

答案 0 :(得分:1)

如果你设置一个断点并在调试模式下运行它,你应该看到它正在退出循环的确切原因。看起来很好,如果有的话,你可能会在你的split()上得到一个例外。

相关问题