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

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!!