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 (Retired) Creating the MVP Prompting for Guesses

Jeremy Hajduk
Jeremy Hajduk
7,245 Points

Getting 'cannot find symbol' error on 'Game'. [SOLVED]

public class Hangman {

    public static void main(String[] args) {
        // Enter amazing code 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("Whoops, that was a miss");
        }     
    }
} 
public class Game {
  private String mAnswer;
  private String mHits;
  private String mMisses;

  public Game(String answer) {
    mAnswer = answer;
    mHits = "";
    mMisses = "";
  }

  public boolean applyGuess(char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
    return isHit;
  }

}
import java.io.Console;

public class Prompter {
  private Game mGame;

  public Prompter(Game game) {
    mGame = game;
  }

  public boolean promptForGuess() {
    Console console = System.console(); 
    String guessAsString = console.readLine("Enter a letter:  ");
    char guess = guessAsString.charAt(0);
    return mGame.applyGuess(guess);                                              
  }
}
public class Game {
  private String mAnswer;
  private String mHits;
  private String mMisses;

  public Game(String answer) {
    mAnswer = answer;
    mHits = "";
    mMisses = "";
  }

  public boolean applyGuess(char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
    return isHit;
  }

}
Hangman.java:5: error: cannot find symbol                                                                                                                   
        Game game = new Game("treehouse");                                                                                                                  
                        ^                                                                                                                                   
  symbol:   class Game                                                                                                                                      
  location: class Hangman                                                                                                                                   
./Prompter.java:4: error: cannot find symbol                                                                                                                
  private Game mGame;                                                                                                                                       
          ^                                                                                                                                                 
  symbol:   class Game                                                                                                                                      
  location: class Prompter                                                                                                                                  
./Prompter.java:6: error: cannot find symbol                                                                                                                
  public Prompter(Game game) {                                                                                                                              
                  ^                                                                                                                                         
  symbol:   class Game                                                                                                                                      
  location: class Prompter                                                                                                                                  
4 errors

3 Answers

Marileen Mennerich
Marileen Mennerich
1,161 Points

Everything looks fine to me. It still seems your Hangman and Prompter classes can not see/find the Game class. Is there an option to import it, in case they are not within the same package? The the Game class' file name "Game.java" (with a capital G)?

Jeremy Hajduk
Jeremy Hajduk
7,245 Points

I deleted 'Game.java' and copied the code into a new file with the same name and now it's working. I don't know what went wrong, but that solved it. Thanks for your response!

Shawn Dyas
Shawn Dyas
543 Points

Copying Game.java code, deleting Game.java and pasting into back resolved my issue as well.

That worked for me as well