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 trialBrighton Muungani
1,687 PointsCannot find symbol error
Have been following Craig in coding the Hangman game but got a Cannot find symbol error. What does this error require coz I have been trying but it aint working. Below is the code:
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);
System.out.print("Enter a letter: ");
String guessInput = scanner.nextLine();
char guess = guessInput.charAt(0);
return game.applyGuess(guess);
}
}
Justin Horner
Treehouse Guest TeacherHello Brighton,
Would you mind sharing your code for Game.java
and Hangman.java
as well?
4 Answers
Justin Horner
Treehouse Guest TeacherHello Brighton,
Thank you for providing your code for the other files. It seems you're Game class in Game.java is missing the applyGuess
method that Prompter.java depends on. The error is happening on the last line of promptForGuess.
return game.applyGuess(guess);
Once you add this method to the Game class, you should be able to compile without errors. Follow along in this video Storing Guesses to see Craig add this method in Game.java.
class Game {
private String answer;
private String hits;
private String misses;
public Game(String answer){
this.answer=answer;
}
public boolean applyGuess(char letter) {
boolean isHit = answer.indexOf(letter) != -1;
if (isHit) {
hits += letter;
} else {
misses += letter;
}
return isHit;
}
}
I hope this helps.
Zachary Kaufman
1,463 PointsWhere is the applyGuess() method? You call it but don't have it in your code.
Brighton Muungani
1,687 PointsThanx Justin, below are the respective codes
Game.java code
class Game{
private String answer;
public Game(String answer){
this.answer=answer;
}
}
Hangman.java code
public class Hangman {
public static void main(String[] args) {
// Your incredible code goes here...
Game game = new Game("treehouse");
Prompter prompter = new Prompter(game);
boolean isHit = prompter.promptForGuess();
if (isHit){
System.out.println("We got a hit!");
}else {
System.out.println("Oops missed!");
}
}
}
Brighton Muungani
1,687 PointsYah it did, thanx very much Justin
Justin Horner
Treehouse Guest TeacherYou're welcome!
matth89
17,826 Pointsmatth89
17,826 PointsEdited for formatting.