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

i tried to make the hang man game step by step on netbeans but it doesnt let me play...

it shows the build was seccessful but doesnt let me enter the first word... can someone tell me why is that and how to fix it... thanks up front

public class HangMan { public static void main(String[] args) { // TODO code application logic here if(args.length == 0){ System.out.println("Please enter a word : "); System.exit(0); } Game game = new Game(args[0]); Prompter prompter = new Prompter(game); prompter.play(); }

}

public class Game { public static final int MAX_MISSES = 7; private String mAnswer; private String mHits; private String mMisses;

public Game(String answer){
    mAnswer = answer;
    mHits = "";
    mMisses = "";
}

private char validateGuess(char letter){
    if(!Character.isLetter(letter)){
        throw new IllegalArgumentException("A letter is required");
    }
    letter = Character.toLowerCase(letter);
    if(mMisses.indexOf(letter) >= 0 || mHits.indexOf(letter) >=0){
        throw new 
        IllegalArgumentException(letter + "had already been guessed");
    }
    return letter;
}

public boolean applyGuess(String letters){
    if(letters.length() == 0){
        throw new IllegalArgumentException("No letter found");
    }
    return applyGuess(letters.charAt(0));
}

public boolean applyGuess(char letter){
    letter = validateGuess(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;
}

public int getRemainingTries(){
    return MAX_MISSES - mMisses.length();
}

public String getAnswer(){
    return mAnswer;
}

public boolean isSolved(){
    return getCurrentProgress().indexOf('-') == -1;
}

}

import java.io.Console;

public class Prompter { private Game mGame;

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

public void play(){
    while(mGame.getRemainingTries() > 0 && !mGame.isSolved()){
        displayProgress();
        promptForGuess();
    }
    if(mGame.isSolved()){
        System.out.printf("Congratulations you won with %d tries left.\n",
                           mGame.getRemainingTries());
    } else {
        System.out.printf("Bummer the word was, %s  :(",mGame.getAnswer());
    }
}

public boolean promptForGuess(){
    Console console = System.console();
    boolean isHit = false;
    boolean isValidGuess = false;
    while(!isValidGuess){
    String guessAsString = console.readLine("Enter a letter: ");
    try{
        isHit =  mGame.applyGuess(guessAsString);
        isValidGuess = true;
    }catch (IllegalArgumentException iae){
        console.printf("%s. Please try again.\n",iae.getMessage());
    }
    }//while 
    return isHit;
}

public void displayProgress(){
    System.out.printf("You have %d tries left to solve: %s",
                       mGame.getRemainingTries(),
                       mGame.getCurrentProgress());
}

}

Please share you code snippet here to check whats wrong.

Dane Parchment
Dane Parchment
Treehouse Moderator 11,077 Points

We cannot help you if you do not show the code you used to create the hangman game.

1 Answer

The error is probably NullPointerException

Remember in IDE you cannot use Console.

console.readLine()

If you want to know why, read this awesome explanation on StackOverflow

http://stackoverflow.com/questions/26470972/trying-to-read-from-the-console-in-java

If you have other errors, please post them.

And also please if you can refactor the code according to markdown cheatsheet, it is super hard to copy/paste it to check for other errors.

In order to paste code you make one empty line before, one empty line after, also three backticks in the beginning, and three backticks at the end. In the beginning backticks please write "java". It is all in cheatsheet.

It seems like you tried, but it didn't quite work.

thank you ! i done it with the Scanner library and it doesnt work as well... the out put when i run the program is 'run: Please enter a word BUILD SUCCESSFUL (total time: 0 seconds)

Well, I cannot help you if you don't paste your code.

Could you share project on GitHub and post link here?

It is the best way. Better than just pasting code here.