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) Delivering the MVP Determining if It Is Solved

I can't understand this error.

Workspaces:

Prompter.java:

import java.io.Console;

public class Prompter {
  private static Game mGame;

  public Prompter(Game game) {
    mGame = game;
  }
  public void play() {
    while(mGame.getRemainingTries() > 0 && !mGame.isSolved) {
      displayProgress();
      promptForGuess();
    }
    if (mGame.isSolved()) {
      System.out.printf("Congratulations,you won with %d tries remaining\n", mGame.getRemainingTries());


    } else {
      System.out.printf("Bummer, the word was %s.  :(\n"
                        mGame.getAnswer());

    }
  }

  public static boolean promptForGuess() {
    Console console = System.console();
    boolean isHit = false;
    boolean isValidGuess = false;
    while (! isValidGuess) {
      String guessAsString = console.readLine("Enter a letter: ");

      try {
        isHit = mGame.applyGuess(guessAsString);
        isValidGuess = true;
      } catch (IllegalArgumentException iae) {
        console.printf("%s. Please try again.\n", iae.getMessage());
      }
  }
    return isHit;
}

  public void displayProgress() {
    System.out.printf("You have %d tries left to solve:  %s\n",
                      mGame.getRemainingTries(),
                      mGame.getCurrentProgress());
  }

}

Hangman.java:

public class Hangman {

    public static void main(String[] args) {
        // Enter amazing code here:
      Game game = new Game("treehouse");
      Prompter prompter = new Prompter(game);
      prompter.play();
    }

}

Game.java:

public class Game {
  public static final int MAX_MISSES = 7;
  private String mAnswer;
  private String mHits;
  private String mMisses;

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

  }
  public String getAnswer() {
    return mAnswer;
  }

  private char validateGuess(char letter) {
    if(! Character.isLetter(letter)) {
      throw new IllegalArgumentException("A letter is required");
    }
    letter = Character.toLowerCase(letter);
    if (mMisses.indexOf(letter) >= 0 || mHits.indexOf(letter) >= 0) {
      throw new IllegalArgumentException(letter + "has already been guessed");
    }
    return letter;
  }

  public boolean applyGuess (String letters) {
    if (letters.length() == 0) {
      throw new IllegalArgumentException("No letter found");
    }
    return applyGuess(letters.charAt(0));
  }



  public boolean applyGuess(char letter) {
    letter = validateGuess(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;
}


public int getRemainingTries() {
  return MAX_MISSES - mMisses.length();
}

  public String getAnswer() {
    return mAnswer;
  }

  public boolean isSolved() {
    return getCurrentProgress().indexOf('-') == -1;
  }


}

Errors:

./Prompter.java:20: error: illegal start of expression              
                        mGame.getAnswer());                         
                             ^                                      
./Prompter.java:20: error: ';' expected                             
                        mGame.getAnswer());                         
                                       ^                            
./Prompter.java:10: error: cannot find symbol                       
    while(mGame.getRemainingTries() > 0 && !mGame.isSolved) {       
                                                 ^                  
  symbol:   variable isSolved                                       
  location: variable mGame of type Game                             
5 errors                        

3 Answers

Chris Hinton
PLUS
Chris Hinton
Courses Plus Student 12,588 Points

Hi Isaiah, it looks like you're missing a comma on the end of your printf statement on line 19 of Prompter.java.

Could that be it?

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

isSolved is a method, it should be called. mGame.isSolved().

Hope it helps!

I get this error now:

./Game.java:65: error: method getAnswer() is already defined in class Game                                                      
  public String getAnswer() {                    
                ^    
1 error
Craig Dennis
Craig Dennis
Treehouse Teacher

getAnswer is defined twice...once at the top and once at the bottom.

Thanks! :+1: :satisfied: