Java Java Objects Delivering the MVP Arrays and Command Line Arguments

I still dont fully understand how craig implemented that when we start the program the program get a word by us

Hi, in this video craig added this code to the HANGMAN.JAVA file :

public class Hangman

  public static void main(String[] args)
    if(args.length == 0)
      System.out.println("Usage: java Hangman <answer>");
      System.out.println("answer is required");
    // Your incredible code goes here...
    Game game = new Game(args[0]);

what exactly the if statement does?, i dont fully understand the explanation about the args , and why after that when we create a game object instance we pass in args[0]?>> i understand that when we run "javac && java hangman" we need to add another word to it in order to let the program take the word but how is it working? thanks!

Eric McKibbin Steven Parker

1 Answer

Steven Parker
Steven Parker
200,785 Points

:bell: Hi, I was alerted by your tag.

The "args" array contains strings holding the arguments that were supplied on the command line. Since we require at least one, that first one can be accessed as args[0]; and it is passed to the Game instance to be used as the word to be guessed.