Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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");
      System.exit(1);
    }
    // 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 hangman.java && 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
230,995 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.