简体   繁体   中英

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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