0
public static void main(String[] args) {

    char play = 0;

    System.out.println("Welcome to Tic-Tac-Toe!!");
    System.out.print("Would you like to play a game? (enter 'y' for yes or 'n' for no): ");
    play = keyboard.next().charAt(0); 

    if (play != 'y') {
        System.out.println("Goodbye!");
    }

    playGame();

    System.out.print("Would you like to play another game (enter 'y' for yes or 'n' for no): ");
    play = keyboard.next().charAt(0);

    if (play != 'y') {
        System.out.println("Goodbye!");
    }
}

// *******************************************************
private static void playGame() {    

    String move;

      System.out.print("Who should move first? (c=computer h=human): ");
      move = keyboard.nextLine();

      move = move.toLowerCase();

      while ( !move.equals("c") && !move.equals("h")) {
          System.out.println("'" + move + "'"+ " is not a valid option.");
          System.out.print("Who should move first? (c=computer h=human): ");
          move =keyboard.nextLine();
          move = move.toLowerCase();
     }
      System.out.print("The computer is X, the human is O");
      if (move.equals("c")) {
          char currentPlayer = 'c';
      } else if (move.equals("h")) {
          char currentPlayer = 'h';
      }

    char currentPlayer = ' ';

This method asks the user who should play first (tic tac toe), and then they type either "c" or "h" to play first. If anything else is typed then it will loop until one of those chars are typed. I have other code above in another method that asks if they want to play a game, would that effect this method?

example:

Welcome to Tic-Tac-Toe!!

Would you like to play a game? (enter 'y' for yes or 'n' for no): y

Who should move first? (c=computer h=human): '' is not a valid option. this right here is the problem

Who should move first? (c=computer h=human): c

The computer is X, the human is O

  • Yes, the earlier input may be causing this behavior (depending how you call it; you don't show that code in your question). See [here](https://stackoverflow.com/questions/29640718/user-input-not-working-with-keyboard-nextline-and-string-java). – 0x5453 Apr 26 '18 at 21:40
  • 1
    You're asking us what effect _some method that you haven't shown us_ is likely to have? I think this might be on-topic at clairvoyance.stackexchange.com. – Dawood ibn Kareem Apr 26 '18 at 21:42
  • Having said that, your problem is probably that you're using `keyboard.next()`, instead of `keyboard.nextLine()` when you ask if they would like to play a game. Calling `keyboard.next()` does not consume the newline character at the end of the line, which means that the following call to `keyboard.nextLine()` returns an empty string. – Dawood ibn Kareem Apr 26 '18 at 21:44
  • @0x5453 I added the method before it – Luke Mihalovich Apr 26 '18 at 22:03
  • @DawoodibnKareem yes, that was it. Thank you – Luke Mihalovich Apr 26 '18 at 22:09

1 Answers1

-2

It's probably because you're checking before they ever type anything. so

"" (empty string) != "c" or "h"

and you get your error message.

Dave2e
  • 15,736
  • 17
  • 32
  • 37
casey
  • 1
  • 2