I'm writing a simple tic tac toe game that is player vs. computer. For my method playerMakeTurn, I want the player to first enter the row they will make their move in, and then the column in that row. However, I keep getting the following:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at TicTacToe.playerMakeTurn(TicTacToe.java:62)
at TicTacToe.main(TicTacToe.java:17)
I have tried implementing hasNextInt() but my implementation is most likely incorrect.
public static String[][] playerMakeTurn (String[][]playGrid)
{
boolean validMove = false;
Scanner in = new Scanner (System.in);
while (validMove != true)
{
System.out.println("Make your move: enter row number (top to bottom; 1-3)");
int rowMove = in.nextInt() - 1;
System.out.println("Make your move: enter column number for row " + (rowMove + 1) + ": (left to right; 1-3)");
int colMove = in.nextInt() - 1;
if (playGrid[rowMove][colMove] == "-");
{
playGrid[rowMove][colMove] = "X";
validMove = true;
}
}
return playGrid;
}
I expect for it to print the first statement, I input the row value. Then it prints the next and I input the column value. I've read on similar answers but frankly I do not understand them. What would the right version of my code look like.