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
ibrahim Warsame
7,803 PointsWhat is wrong? My hangman game wont work.
My Hangman game wont work.
When i compile the code like this in the console:
Javac Hangman.java
It does not return any errors, but when I try to execute the file like this:
java Hangman
My code wont run and work. I have no idea what is wrong. Can anyone look at my source code.
File Prompter.java:
import java.io.Console;
public class Prompter {
private Game mGame;
public Prompter(Game game) {
mGame = game;
}
public void play() {
while(mGame.getRemainingTries > 0 ) {
displayProgress();
promptForGuess();
}
}
public boolean promptForGuess() {
Console console = System.console();
String guessAsString = console.readLine("Enter your guess: ");
char guess = guessAsString.charAt(0);
return mGame.applyGuess(guess);
}
File Game.java :
public class Game {
public static final int MAX_MISSES = 7;
private static String mAnswer;
public static String mHits;
public String mMisses;
private int mManyTimes;
public char letter;
public Game(String answer) {
mAnswer = answer;
mHits = "";
mMisses = "";
}
public boolean applyGuess(char letter) {
boolean isHit = mAnswer.indexOf(letter) >= 0;
if (isHit) {
mHits = mHits + letter;
} else {
mMisses = mMisses + letter;
}
return isHit;
}
public static String howManyRight() {
String hitler = "";
for (char letter: mAnswer.toCharArray()) {
char display = '_';
if(mHits.indexOf(letter) >= 0) {
display = letter;
}
The class that holds the main method:
public class Hangman {
public static void main(String[] args) {
Game game = new Game("treehouse");
Prompter prompter = new Prompter(game);
prompter.play();
}
}
I tried to solve this problem without help but it didn’t work Thanks for advance :)
2 Answers
Steve Hunter
57,712 PointsHi Ibrahim,
When you say "it won't work", do you get any errors displayed at run time? Can you paste those into here?
And, as Geovanie pointed out, there's some missing code we could do with seeing too.
Steve.
Geovanie Alvarez
21,500 PointsYour code is incomplete i can't find the displayProgress method on the Prompter.java and the getRemainingTries method in the Game.java
ibrahim Warsame
7,803 Pointsibrahim Warsame
7,803 PointsI get no errors. I don’t know what I’m doing wrong.
ibrahim Warsame
7,803 Pointsibrahim Warsame
7,803 PointsI forgot to copy the displayProgress() method but it is there.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsOK, so what's the issue? You say "it won't run or work" - what happens when you run your code?
Steve.
ibrahim Warsame
7,803 Pointsibrahim Warsame
7,803 PointsWell I somehow managed to fix the problem. its is really weird how this works, sometimes it works just fine and sometimes not. Maybe it is only me :( anyway thanks for trying to help me
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHey, no problem. Shout if you need any more.