0

So I'm working on a very basic java assignment, and I believe I understand how the (String [] args) part of the main works. Basically my understanding is that the command line arguments are put into the array of strings called args. However my code won't produce the results I expect. It seems to work fine with no arguments but when I put in either of the other options I'm always greeted with my usage error.

class TicTacToe
{
  public char[][] gameboard = {  {' ', ' ', ' '},
              {' ', ' ', ' '},
              {' ', ' ', ' '}  };

  public static void main(String [] args)  {
    char play1, play2; 

    if(args.length == 0)  {
      play1 = 'h';
      play2 = 'h';
    }else if(args.length == 1 && args[0] == "-c")  {
      play1 = 'c';
      play2 = 'c';
    }else if(args.length == 2 && args[1] == "1")  {
      play1 = 'c';
      play2 = 'h';
    }else if(args.length == 2 && args[1] == "2")  {
      play1 = 'h';
      play2 = 'c';
    }else{
      System.out.println("Usage: java TicTacToe [-c [1|2]]");
      return;
    }
  }
}
  • The args may include any variable when you call to the main method from outside of the program. The code you have wrote is running the main method without any parameter that is why the args always empty. – Rotem Oct 11 '16 at 16:17

1 Answers1

0

You are comparing strings with '=='

You should compare strings with .equals()

Giancarlo Ventura
  • 849
  • 1
  • 10
  • 27