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

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

Hangman error messages. Can't find the errors.

Thought my code matched what's on video but I get the following errors :

reached eof while parsing; identifier expected import.java.io.Console; package error.java.io does not exist import.java.io.Console; Hangman.java error cannot find symbol; Prompter.java error identifier expected import.java.io.Console; Prompter reached eof while parsing.

public class Hangman {

    public static void main(String[] args) {
        // Enter amazing code here:
        Game game = new Game("treehouse");
        Prompter prompter = new Prompter(game); //pass game object into Prompter
        boolean isHit = prompter.promptForGuess();
        if (isHit) {
          System.out.println("We got a hit"); }
        else {
          System.out.println("Whooops 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) {//method
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }

    return isHit; 

}

import.java.io.Console;

public class Prompter {// object to do i/o
  private Game mGame;

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

  public boolean prompForGuess() {
    Console console = System.console();// Console is a class importing from java.io
    String guessAsString = console.readLine("Enter a letter: ");
    char guess = guessAsString.charAt(0);
    return mGame.applyGuess(guess);//call applyGuess method, pass in guess(1st char from string)return T/F
}

2 Answers

OK - a few points here:

  1. Well done for creating a variable named isHit - that's awesome work. ;-)
  2. This public boolean prompForGuess() looks fishy - you're calling it with boolean isHit = prompter.promptForGuess(); Typo there, perhaps.
  3. You need a space after import, not a dot import.java.io.Console;
  4. You need a curly brace after the last one in Prompter - you've not closed out the class, which is why it ran out of file when parsing.
  5. Same after applyGuess(); missing closing brace in the Game class, I think

I think you've missed a couple of braces and have a couple of typos. Else, all looks good!

Steve.