0

Im creating a little game to practice java and want to name 2 People easy peasy with my definePlayer method. Why is Eclipse throwing this no line found error?

public String definePlayer(){
    Scanner sc = new Scanner(System.in);

    System.out.println("Define Player Name: ");
    String playerName = sc.nextLine();
    sc.close();
    System.out.println("Player '" + playerName + "' defined");
    return playerName;
}



public static void main(String[] args) {
    navalWar game = new navalWar();

    int[] fieldArray = game.fieldArray();
    game.printField(fieldArray);

    String Player1 = game.definePlayer();
    String Player2 = game.definePlayer();

Error:

After the second defining:

Exception in thread "main" Define Player Name: 
java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at navalWar.navalWar.definePlayer(navalWar.java:36)
    at navalWar.navalWar.main(navalWar.java:51)
Richie
  • 73
  • 1
  • 6
  • refer answer for this one : https://stackoverflow.com/questions/43964764/scanner-nextline-exception-no-line-found thats your solution – Ajanth Jun 09 '18 at 19:39
  • I know when I delete sc.close() it works but I dont really know why. I create a scanner and close it then create a new one which shouldnt interfere with the other one ? – Richie Jun 09 '18 at 19:45

0 Answers0