0

I'm building a program to allow two players to play Tic-Tac-Tie. I'm using a while loop to keep the game going until a winner is found, but on the second iteration I'm getting below exception:

Exception in thread "main" java.util.NoSuchElementException

Java Program

import java.util.Scanner;

public class Game {
public static void main(String[] args) {
    Board board1 = new Board();
    System.out.println("Game Starts:");
    board1.showBoard();
    while (board1.checkWin()!="X" && board1.checkWin()!="O") {
        System.out.print("What cell (0-8) do you want to play in:");
        Scanner sc = new Scanner(System.in);
        int i;
        i=sc.nextInt();
        System.out.print("What player are you:");
        Scanner sc1 = new Scanner(System.in);
        String s = sc1.nextLine();
        sc1.close();
        sc.close();
        char c=s.charAt(0);
        board1.makeMove(i, c);
        board1.showBoard();
        System.out.println();
        if(board1.checkWin()!="Draw") {
            System.out.print("Winner is " + (board1.checkWin()));
        }
    }
}
}

Game Starts:

. . .
. . .
. . .
What cell (0-8) do you want to play in:8
What player are you:X
. . .
. . .
. . X"

What cell (0-8) do you want to play in:Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Game.main(Game.java:12)
Morse
  • 8,258
  • 7
  • 39
  • 64

2 Answers2

-1

You do not need two scanner objects to get the input. one scanner object outside the loop is sufficient to read the input from the keyboard. it is a buffer element that just reads input and assigns it to the variable you want it to be in.

public class Game {
public static void main(String[] args) {
    Board board1 = new Board();
    System.out.println("Game Starts:");
    board1.showBoard();

    Scanner sc = new Scanner(System.in);

    while (board1.checkWin()!="X" && board1.checkWin()!="O") {
        System.out.print("What cell (0-8) do you want to play in:");
        int i;
        i=sc.nextInt();
        System.out.print("What player are you:");
        String s = sc.nextLine();
        char c=s.charAt(0);
        board1.makeMove(i, c);
        board1.showBoard();
        System.out.println();
        if(board1.checkWin()!="Draw") {
            System.out.print("Winner is " + (board1.checkWin()));
        }
    }
    sc.close();
}
Frankenstein
  • 76
  • 1
  • 11
-1

You are initializing two Scanners with System.in. You can achieve what you need in a single Scanner. If you still like to have two Scanner object, (I don't see any use case of this) then close the first Scanner before creating the second one.

Also, it's a good approach to open or close a resource outside the loop. See the below code where Scanner object is created outside the loop.

    import java.util.Scanner;
    public class Game {

    public static void main(String[] args) {
    Board board1 = new Board();
    System.out.println("Game Starts:");
    board1.showBoard();
    Scanner sc = new Scanner(System.in);
    while (board1.checkWin()!="X" && board1.checkWin()!="O") {
        System.out.print("What cell (0-8) do you want to play in:");
        int i;
        i=sc.nextInt();
        sc.nextLine();// Reads the current line after number
        System.out.print("What player are you:");
        //Scanner sc1 = new Scanner(System.in); Not needed
        String s = sc.nextLine();
        //sc1.close();

        char c=s.charAt(0);
        board1.makeMove(i, c);
        board1.showBoard();
        System.out.println();
        if(board1.checkWin()!="Draw") {
            System.out.print("Winner is " + (board1.checkWin()));
        }
    }
    sc.close();
}
}
Praful Surve
  • 788
  • 1
  • 10
  • 22
  • do you know why when I made these changes I get? Game Starts: - - - - - - - - - What cell (0-8) do you want to play in:8 What player are you:Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at Game.main(Game.java:15) – aster72 Jan 31 '19 at 23:04
  • Ah, you are using nextInt() which reads the number from the inputstream and not the return (end of line), when you call nextLine(), it reads the same line after number which is nothing so it returns an empty String. and you are trying to call charAt(0) for empty string, which is resulting the StringIndexOutOfBoundsException. Call sc.nextLine() twice, edited the answer – Praful Surve Jan 31 '19 at 23:14