-1

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.

  • You should re-use the same scanner for both nextInt() calls. Define one outside your loop and use it multiple times. – Minn Feb 17 '19 at 21:19
  • You should use primitive types when you can (`boolean` instead of `Boolean`). You should create only one scanner, not two scanners every iteration of the loop. – RaminS Feb 17 '19 at 21:20
  • 2
    Possible duplicate of [Java Multiple Scanners](https://stackoverflow.com/questions/19766566/java-multiple-scanners) – George Z. Feb 17 '19 at 21:21
  • I edited it so that the scanner is outside of the loop. The issue still persists. Additionally, this method is part of a loop which contains another scanner use as well. – Maxim Arkhipov Feb 17 '19 at 21:35
  • 1
    Warning: you are comparing Strings with `==`. Never do that; use `.equals()` instead. See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – MC Emperor Feb 17 '19 at 23:49
  • I want to second the comment of MC Emperor. The comparison will most likely work in your specific case but you must not rely on that. use `.equals()` instead. – Angelo Fuchs Feb 18 '19 at 15:39

1 Answers1

0

This code works fine, your problem must be outside of the code shown. I made a MVCE that executes the code (see below) and it does not throw your exception when run (I tried for all 9 inputs).

import java.io.*;
import java.util.*;
public class Test {
   public static void main(String[] args) {
      playerMakeTurn(new String[3][3]);
   }
   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;
    }
}
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72