Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP Current Progress

Whitney Barber
Whitney Barber
9,453 Points

Getting an error message when I attempt to compile and run hangman

ERROR MESSAGE

./Game.java:24: error: class, interface, or enum expected
public String getCurrentProgress() {
^
./Game.java:26: error: class, interface, or enum expected
for (char letter : mAnswer.toCharArray()) {
^
./Game.java:28: error: class, interface, or enum expected
if (mHits.indexOf(letter) >= 0) {
^
./Game.java:30: error: class, interface, or enum expected
}
^
./Game.java:32: error: class, interface, or enum expected
}
^
./Game.java:34: error: class, interface, or enum expected
}
^
./Prompter.java:18: error: cannot find symbol
System.out.printf("Try to solve: %s\n", mGame.getCurrentProgress());
^
symbol: method getCurrentProgress()
location: variable mGame of type Game
7 errors

CODE HANGMAN

public class Hangman {

public static void main(String[] args) {
    // Enter amazing code here:
  Game game = new Game("treehouse");
  Prompter prompter = new Prompter(game);
  prompter.displayProgress();
  boolean isHit = prompter.promptForGuess();
  if (isHit) {
    System.out.println("We got a hit!");
  } else {
    System.out.println("Whoops, that was a miss....");
  }
  prompter.displayProgress();
}

}

CODE GAME

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

CODE PROMPTER

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 void displayProgress() { System.out.printf("Try to solve: %s\n", mGame.getCurrentProgress()); } }

2 Answers

It looks like you have one too many closing brackets at the end of the applyGuess function, which is closing the definition of your Game class. Move a closing bracket from the end of the applyGuess function to the end of the getCurrentProgress function and see if it works.

Whitney Barber
Whitney Barber
9,453 Points

That fixed it! Most of my mistakes are misplaced brackets, I have to work on catching that. Thank you very much!