Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

I get an error message while compiling Hangman.

While creating hangman following the all the instructions, i got an error cannot find symbol in the prompter.displayOutcome, i compared my code with Craig's several times but i. don't seem to find the issue.

Hangman.java: public class Hangman {

public static void main(String[] args) {

    Game game = new Game("treehouse");
    while (game.getRemainingTries() > 0 && !game.isWon())  {

        Prompter prompter = new Prompter(game);
        prompter.displayProgress();
        prompter.promptForGuess();
    }

    prompter.displayOutcome();
}

}

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();
        char guess = guessInput.charAt(0);
        try {
            isHit = game.applyGuess(guess);
            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 have won with %d tries left. %n",
            game.getRemainingTries());
    } else {
        System.out.printf("Sorry, you lost. the word was: %s",
            game.getAnswer());
    }
}

}

1 Answer

Fahad Mutair
Fahad Mutair
10,359 Points

you have to declare Prompter prompter = new Prompter(game); outside the while loop

Prompter prompter = new Prompter(game);
while (game.getRemainingTries() > 0 && !game.isWon())  {
        prompter.displayProgress();
        prompter.promptForGuess();
    }

    prompter.displayOutcome();

This worked, thank you!!