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

Attempting to run Hangman program.

I am attempting to run my hangman program from Intelli J Idea and the teamtreehouse setup. For some reason the program is no longer running like it normally did. It seems to be printing requires <answer>. Any help would be greatly appreciated.

Prompter.java:

import java.util.Scanner;

 class Prompter {
  private Game game;

  public Prompter(Game game) {
   this.game = game;
  }

  public boolean promptForGuess() {
      Scanner scanner = new Scanner(System.in);  
      boolean isHit = false;
      boolean isAcceptable = false;

    do {
      System.out.print("Enter a letter:  ");
      String guessInput = scanner.nextLine();

      try {
        isHit = game.applyGuess(guessInput);
        isAcceptable = true;
      } catch(IllegalArgumentException iae) {
          System.out.printf("%s. Please try again. %n",
                            iae.getMessage());
      }
    } while(! isAcceptable);
    return isHit;
  }

   public void displayProgress() {
     System.out.printf("You have %d tries left to solve:  %s%n",
                       game.getRemainingTries(),
                       game.getCurrentProgress());
   }

   public void displayOutCome() {
     if (game.isWon()) {
       System.out.printf("Congratulations you won with %d tries remaining.%n",
                         game.getRemainingTries());
     }  else {
       System.out.printf("Bummer the word was %s. :( %n",
                         game.getAnswer());
     }
   }
 }

Hangman.java:

public class Hangman {

  public static void main(String[] args) {
    // Your amazing code goes here...
    if (args.length == 0) {
      System.out.println("Usage: java Hangman <answer>");
      System.err.println("answer is required");
      System.exit(1);
    }
    Game game = new Game(args[0]);
    Prompter prompter = new Prompter(game);
    while (game.getRemainingTries() > 0 && !game.isWon()) {
      prompter.displayProgress();
      prompter.promptForGuess();
    }
    prompter.displayOutCome();
  }
}```

Game.java:



```class Game {
  public static final int MAX_MISSES = 7;
  private String answer;  
  private String hits;
  private String misses;

  public Game(String answer) {
   this.answer = answer.toLowerCase(); //talks about instance vs argument passed in ^.
   hits = "";
   misses = "";
  }

  public String getAnswer() {
    return answer;
  }

  private char normalizeGuess(char letter) {
    if (! Character.isLetter(letter)) {
      throw new IllegalArgumentException("A letter is required");
  }
    letter = Character.toLowerCase(letter);
      if (misses.indexOf(letter) != -1 || hits.indexOf(letter) != -1) {
      throw new IllegalArgumentException(letter + " has already been guessed");
    }
    return letter;
  }

  public boolean applyGuess(String letters) {
    if (letters.length() == 0) {
      throw new IllegalArgumentException("No letter found");
  }
        return applyGuess(letters.charAt(0));
  }

  public boolean applyGuess(char letter) {
    letter = normalizeGuess(letter);
    boolean isHit = answer.indexOf(letter) != -1;
      if (isHit) {
        hits += letter;
    }   else {
        misses += letter;
    }
      return isHit;
  }
    public int getRemainingTries() {
      return MAX_MISSES - misses.length();
  }

    public String getCurrentProgress() {
       String progress = "";
        for (char letter : answer.toCharArray()) {
          char display = '-';
          if (hits.indexOf(letter) != -1) {
            display = letter;
              }
              progress += display;
             }
              return progress;
           }
  public boolean isWon() {
   return getCurrentProgress().indexOf('-') == -1;
  }
}```

1 Answer

I imagine you've been running the code from the Treehouse workspace before, by typing something like:

java Hangman ARandomWord

When you click "Run" in IntelliJ, what happens behind the curtains is this:

java Hangman

So your Hangman program has no word to make you guess and is trying to tell you that you need to add an argument so it can work properly. Look at your 'main' method: it checks if the String array args contains at least one String, if it doesn't it prints the message you're seeing. Rather than using IntelliJ to run this program, compile and run it from your computer's terminal. Much easier! If you really want IntelliJ, it has its own terminal that you can open by going to View > Tool windows > Terminal Don't forget that you first have to navigate to the right folder before you type the command 'java Hangman AWordToGuess', or it won't work.

But really I'd advise you to leave IntelliJ be for now. At this stage it's not really helpful to use an IDE. The IDE tries to help you by doing stuff for you, but as long as you don't know exactly what that stuff is that happens in the background, it's just confusing.