0

Eclipse says the scanner is never closed, and prompts me to suppress it, but it is closed.

public boolean hasPlayerAlreadyJoined(Player p) {
    try {
        Scanner scanner = new Scanner(joinedPlayers);
        while (scanner.hasNextLine()) {
            String name = scanner.nextLine();
            if (name.equals(p.getName())) {
                return true;
            }
        }
        scanner.close();
    } catch(Exception e){}
    return false;
}

I am guessing it is because of the while loop but I have never ran into an error like this before. Am I doing something wrong?

1 Answers1

1

put scanner.close(); inside the a finally clause so that it would be called at the end. It's the most optimal way :

Scanner scanner = new Scanner(joinedPlayers);
try {
   ....
}
catch(...) {
   ....
}
finally {
    scanner.close();
}
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67