The code below is suppose to start a connect 4 game and I used the scanner class in order to get the user's input but when I run it, it gives me a NoSuchElementError.
I tried to comment out the close(); method and rerun the program but the same error was prevalent.
Heres my code.
Pick a row
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at E.start(E.java:223)
at Main.main(Main.java:6)
public void start(){//need a method that changes the board{
while(checkPlayerWin()== false|| checkAIWin()== false){
Scanner scan= new Scanner(System.in);
System.out.print("Pick a row ");
int row= scan.nextInt();
insertCounter(row, "X");
scan.close();
reprintBoard();
random();//insert random counter into a place in the board
reprintBoard();
}
}
a.start();//in the class main on line 6