Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Dorka Tamas
Dorka Tamas
5,085 Points

Bash: hello : command not found - Java Hangman

Hi,

I am facing some issues with Hangman game, what I cannot really figure out. When I start to run the game, it stops after I enter a word and says Bash: hello : command not found. Could you please help? Thanks a lot...

Game.java
public class Game {
    public static final int MAX_MISSES = 7;
    private String mAnswer; //What is a member variable?
    private String mHits;
    private String mMisses;

    public 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 + " has 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 Game(String answer) { 
        mAnswer = answer;
        mHits = "";
        mMisses = "";
    }

    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;
    }
}
Prompter.java
import java.io.Console;

public class Prompter {
    private Game mGame; //do we need to store?

    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 remaining\n",
                                                    mGame.getRemainingTries());
        } else {
            System.out.printf("Sorry, the word was %s.  :(\n",
                                                 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());
        }
     }   
     return isHit;
    }

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

}
Hangman.java
public class Hangman {

    public static void main(String[] args) {
        // Enter amazing code 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();
    }
}

[MOD: edited code blocks]

7 Answers

Sorry - I didn't make myself clear.

Your main method - the one that starts the app - takes args. You pass these in when you call the runtime java process on the class containing main. So you've typed in the last part of your command line, java Hangman. This has no args. You can add args by adding them inside parentheses like, as I suggested above, Hangman("hello") - that passes the string 'hello' into the process, with that string being held as an arg; as it is the first one, it'll be args[0], the second will be args[1] etc. It is just an array of arguments - a bit like a Saturday night on the Stella. :wink:

Getting back to your app - as you run your code, your first line inside the main method checks to see if any args were passed in. If they weren't, that means args.length == 0 (which it is) and the lines of code inside the if statement are executed. That's where the System.exit(0) line exits the whole process. So, if you did pass an arg to the method at the beginning, args.length would not be zero and that exit(0) line would not be executed. Your application may then run properly!

For the sake of ease; call your program with an args present. Else you need to figure out reading user input and passing that into the Game constructor. That's not hugely difficult, but it is way easier to just pass an argument in:

treehouse:~/workspace$ javac Hangman.java && java Hangman("hello")

Make sense?

Steve.

But, as above, share your workspace and we can walk through the changes needed.

Hi Dorka,

Apologies for the delay in replying; I was at work.

Assuming you've not got through this issue, the error should tell you what line the error occurred on? And when is this error occurring?

That should help me pin down what the issue is. The app seems to be treating a string as a command, so there may be something simple missing like some inverted commas.

Let me know, and I'll help you out. :+1:

Steve.

Yes, change this:

            if (args.length == 0) {
                System.out.println("Please enter a word");
                System.exit(0); // <-- this throws you out of the app!!
            }

If you don't open the application and pass a string to it, the app prompts you for a string - it never reads input and immediately exits the process wth System.exit(0).

You need a readLine() instead of the println and you need to delete the exit command.

Try starting your app with:

javac Hangman.java && java Hangman("hello")

That should work, but might throw another error. Try it and then we'll try to fix your app.

Steve.

Dorka Tamas
Dorka Tamas
5,085 Points

Thanks Steve for the reply! Unfortunately, I could not solve it yet.

It seems to me that it is a runtime error, it does not tell me where it is the problem, It runs, it asks me to enter a word, I entered hello and then it says Bash: hello : command not found.

So I am still confused how to deal with it. :(

Thanks for everything!

Can you post a screenshots of the workspace before and after the error, please?

When you call the Hangman class from the command prompt, are you surrounding the word hello with double quotes? You must treat it as a string - are you doing that? For some reason the word hello is being treated as a keyword, not a string.

Dorka Tamas
Dorka Tamas
5,085 Points

Sorry I cannot figure out how to insert it, but u can read exactly this:

treehouse:~/workspace$ javac Hangman.java && java Hangman Please enter a word treehouse:~/workspace$ hello bash: hello: command not found

Dorka Tamas
Dorka Tamas
5,085 Points

I did not put it with "", but I tried it now, still the same answer.

treehouse:~/workspace$ javac Hangman.java && java Hangman

Please enter a word

treehouse:~/workspace$ hello bash: hello: command not found

Share your workspace - let me have a try at this end.

Stee.

The app is ending after it outputs 'Please enter a word' - that's why you're at the command prompt. Let's see what's wrong there ...

Dorka Tamas
Dorka Tamas
5,085 Points

Hi Steve,

It is working now :)

Thanks a lot for putting so much efforts into it, now I understand the logic behind it! I really appreciate your help!

Hey, no problem; glad to help out! :+1: