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)