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 trialBrian Maimone
Courses Plus Student 1,644 PointsHangman 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
Steve Hunter
57,712 PointsOK - a few points here:
- Well done for creating a variable named
isHit
- that's awesome work. ;-) - This
public boolean prompForGuess()
looks fishy - you're calling it withboolean isHit = prompter.promptForGuess();
Typo there, perhaps. - You need a space after import, not a dot
import.java.io.Console;
- 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. - Same after
applyGuess()
; missing closing brace in theGame
class, I think
I think you've missed a couple of braces and have a couple of typos. Else, all looks good!
Steve.
Brian Maimone
Courses Plus Student 1,644 PointsThanks Steve. Down to the one boolean isHit variable.