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 Current Progress

Safira Nugroho
Safira Nugroho
3,910 Points

error: cannot find symbol?

Hi,

I've tried back and forth trying to compare my code to Craig's and other people's in the discussion board, but I can't seem to find what's the problem, so I hope somebody can help me with this.

Below is my code, which I think is exactly the same with Craig's:

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;
  }

  public String getCurrentProgress() {
   String progress = ""; 
   for (char letter: mAnswer.toCharArray());
    char display = '-';
    if (mHits.indexOf(letter) >= 0) {
      display = letter;
    }
    progress += display;
    return progress;
  } 

}

But when I try to run it in the console, it says:

./Game.java:26: error: cannot find symbol                                   
    if (mHits.indexOf(letter) >= 0) {                                       
                      ^                                                     
  symbol:   variable letter                                                 
  location: class Game                                                      
./Game.java:27: error: cannot find symbol                                   
      display = letter;                                                     
                ^                                                           
  symbol:   variable letter                                                 
  location: class Game                                                      
2 errors                         

Anyone knows what's the problem here? I'd very much appreciate the help!

Mark VonGyer
Mark VonGyer
21,239 Points

Hi there. if you get this error; the majority of the time it relates to a mispelling in your code.

I would check letter in all places it is mentioned to make sure it is spelt consistently.

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Take a peek at your for loop. Something seems a little wonky near the end of it.

meh ignore my answer - Craig made a better approach on your problem it seems. Sry for my jibberish ;P

Safira Nugroho
Safira Nugroho
3,910 Points

Oops! It's always the little things. Thank you! :-)

public String getCurrentProgress() { //your code }

In this method you're using the char var "letter". But actually it doesn't exist there. You should bring it into the method scope as a parameter like you did in:

public boolean applyGuess(char letter)

Miguel Gonzalez Rocha
Miguel Gonzalez Rocha
2,849 Points

Your for loop ends with ";" and you are supposed to open "{}"