0

I am making a connect 4 game for school. I have a player class defining aspects of each player, Board class, "logic" class and game class which runs the game. My Player class has two methods almost identacle to set up each player only difference is the game piece, X or O. The Player method also reads in a registration ID and gamerTag. It runs flawlessly on player1, but I get java.util.NoSuchElementException. I actually get this error when trying to run any method that uses in.next...();

Player Class Methods

public void setPlayer1() {
    Scanner in = new Scanner(System.in);
    System.out.println("\nPlayer 1 please enter your Arena registration ID.");
    this.setID(in.nextLong());
    System.out.print("");
    System.out.println("Player 1 please enter the name you would like to use.");
    this.setTag(in.next());
    System.out.print("");
    System.out.print(gamerTag + " your game piece is \"X\".");
    this.setPiece("X");
    in.close();
}

public void setPlayer2() {
    Scanner in = new Scanner(System.in);
    System.out.println("\nPlayer 2 please enter your Arena registration ID.");
    this.setID(in.nextLong());
    System.out.print("");
    System.out.println("Player 2 please enter the name you would like to use.");
    this.setTag(in.next());
    System.out.print("");
    System.out.print(gamerTag + " your game piece is \"O\".");
    this.setPiece("O");
    in.close();}

public static void main(String[] args) {
    Connect4TextConsole game = new Connect4TextConsole();
    System.out.print(board);
    player1.setPlayer1();
    player2.setPlayer2();
    winner = Connect4.checkForWin(board);
    while (!winner){
        turn = turn.playerTurn(player1, player2);// switch players
        int column = Connect4.askForColumn(turn);

    }
}
KrisEverhart
  • 21
  • 1
  • 4
  • Instead of instantiating multiple Scanners, only create 1 Scanner object (probably in the main method) and pass it around to each of your methods – user May 30 '20 at 18:34
  • how do you pass it around to the other methods? Will they just all see it automatically since they're in the same package? – KrisEverhart May 30 '20 at 18:36
  • No, I meant you add it a parameter `Scanner sc` to each of your methods, although you could always make it a static variable, but that's not considered good practice – user May 30 '20 at 18:36
  • Does this answer your question? [How to use the same Scanner across multiple classes in Java](https://stackoverflow.com/questions/29323116/how-to-use-the-same-scanner-across-multiple-classes-in-java) – user May 30 '20 at 18:37
  • 1
    You are closing the scanner, which in turn closes the underlying input stream – which is stdin. Aften you close this, you are not able to read from it anymore. Don't close stdin. A thumb rule is: **don't close what you didn't open**. – MC Emperor May 30 '20 at 19:25

1 Answers1

0

You just need to give it to the method as an argument

public void setPlayer1(Scanner in) {

    System.out.println("\nPlayer 1 please enter your Arena registration ID.");
    this.setID(in.nextLong());
    System.out.print("");
    System.out.println("Player 1 please enter the name you would like to use.");
    this.setTag(in.next());
    System.out.print("");
    System.out.print(gamerTag + " your game piece is \"X\".");
    this.setPiece("X");

}

public void setPlayer2(Scanner in) {

    System.out.println("\nPlayer 2 please enter your Arena registration ID.");
    this.setID(in.nextLong());
    System.out.print("");
    System.out.println("Player 2 please enter the name you would like to use.");
    this.setTag(in.next());
    System.out.print("");
    System.out.print(gamerTag + " your game piece is \"O\".");
    this.setPiece("O");
    }

`public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Connect4TextConsole game = new Connect4TextConsole();
    System.out.print(board);
//Here u give the scanner ur methods
    player1.setPlayer1(in);
    player2.setPlayer2(in);
    in.close();
    winner = Connect4.checkForWin(board);
    while (!winner){
        turn = turn.playerTurn(player1, player2);// switch players
        int column = Connect4.askForColumn(turn);

    }
}` 
Tanyel
  • 61
  • 7