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 trialJason Laux
3,128 PointsNot displaying correct guesses
When running hangman in console I get:
Try to solve: --------- Enter a letter: t We got a hit! Try to solve: ---------
It seems my progress variable in getCurrentProgress() in Game.java but I can't find problem code. I've compared it several times to Craig's code in the video, but I'm missing something.
Can anybody see where I went wrong?
Here's the code for Game.java
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;
}
}
And here is the code for Prompter.java
import java.io.Console;
public class Prompter {
private Game mGame;
public Prompter(Game game) {
mGame = game;
}
public boolean promptForGuess() {
Console console = System.console(); // calling console() on System will
// return a console object
// This requires a package: java.io.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());
}
}
And finally, 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.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();
}
}
Any feedback would be appreciated.
1 Answer
Stephen Bone
12,359 PointsHi Jason
I think your correct and the issue lies with your getCurrentProgress method. If you pass in a different char such as 'e' does it work?
I'm guessing it does and in that case the issue lies with the line below.
if (mHits.indexOf(letter) > 0) {
The char 't' is at index 0 so this test would fail and the letter 't' won't be assigned to the display variable. It should be greater than or equal to. So your code should look like:
if (mHits.indexOf(letter) >= 0) {
Hope it helps!
Stephen
Jason Laux
3,128 PointsJason Laux
3,128 PointsThanks, Stephen. That worked! I need to pay attention to details. I got caught doing the same thing in the coding challenge following the video.