-2

I'm currently making a Tic Tac Toe game. when a player wins and is asked if they want to play again, I call the Playing(); method to restart the game. The issue I'm having is that it loops the Playing(); without stopping at the first Scanner.

How can I make the loop stop at the first Scanner (which asks the user to enter their [row]?

Output when A player agrees to restart :

Enter [Row] : Please enter a number : Enter [Row] :

Playing() Method

public static void Playing() {

    String line;

    // Check if the user's input is a number. If not, retry!

    while (true) {

        try {
            System.out.print("\nEnter [Row] : "); //Tell user to input Row
            line = input.nextLine();
            row = Integer.parseInt(line) - 1;

            System.out.print("Enter [Col] : "); //Tell user to input Col
            line = input.nextLine();
            col = Integer.parseInt(line) - 1;
            break;
        } catch (NumberFormatException e) {

            System.out.print("Please enter a number : ");
            break;

        }

    }

    //Check if their input for [row][col] is valid
    if (row < 0 || row > 2 || col < 0 || col > 2) {
        System.out.print("Oops! Invalid input. Try again");
        Playing();

    } else if (Board[row][col] != '_') {
        System.out.print("This position is already taken. Try again");
        Playing();

    } else {
        Board[row][col] = player;
        moveCount++;
        DisplayBoard();
        GameOver(); //Check if anyone won. If not, continue the game

    }
}

Replay() Method

    public static void Replay() {
    /*
     * Check if the user wants to play again
     */
    System.out.print("Would you like to play again?(Y or N) : ");
    while (true) {
        String retry = input.next();

        if ("y".equalsIgnoreCase(retry)) {
            for (int r = 0; r < 3; r++) {
                for (int c = 0; c < 3; c++) {
                    Board[r][c] = '_';
                }
            }
            System.out.print("-----------------------------------------------------\n");
            DisplayBoard();
            Playing();
            break;
        } else if ("n".equalsIgnoreCase(retry)) {
            System.out.println("Thank you for Playing!");
            System.exit(1);

            // If the user enters an invalid input, this will ask them to try again
        } else {
            System.out.print("Invalid input. Would you like to play again?(Y or N) : ");

        }
    }
}
Crypto
  • 60
  • 9

1 Answers1

1

First of all, what is the reason for using

line = input.nextLine();
row = Integer.parseInt(line) - 1;

where one can actually get the integers using nextInt() method available from Scanner

line = input.nextInt();
row = line - 1;

And to answer your question, I think this is the culprit which causes to skip your inputs

String retry = input.next();

In this line if your enter some keyword, say "Hello" and hit enter which is "Hello\n" the next method only takes "Hello" and the \n will skip your nextLine() method. So I suggest you to try adding another nextLine() after this line

String retry = input.next();
input.nextLine();

Note: This is only a guess for a problem, I haven't actually debugged your code by running on my end-system.