简体   繁体   English

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

[英]Why is my hasNextLine() while loop only working one time?

I have been working on this for hours and can't find the problem by myself or online. 我已经工作了几个小时,无法自己或在线找到问题。 I dont know why the first loop does work and the second does not proceed past the first itteration. 我不知道为什么第一个循环会起作用,而第二个循环却无法通过第一个发信号。

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);
}

This is the file that I am reading in: 这是我正在阅读的文件:

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

There is no exception, it just doesn't run more than once 也不例外,它只运行了不止一次

If you set a break point and run it in debug mode, you should see exactly why it's exiting the loop. 如果设置一个断点并以调试模式运行它,则应该确切了解为什么它退出循环。 Looks fine to me, if anything, you may be getting an exception on your split(). 在我看来,如果有的话,您的split()可能会出现异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM