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 Java Objects (Retired) Creating the MVP Prompting for Guesses

Sean Flanagan
Sean Flanagan
33,232 Points

More console trouble

Hi.

My console isn't working. Here's 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();
    String guessAsString = console.readLine("Enter a letter: ");
    char guess = guessAsString.charAt(0);
    return mGame.applyGuess(guess);
  }
}

Hangman.java:

import java.io.Console;

public class Prompter {
  private Game mGame;

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

  public boolean promptForGuess() {
    Console console = System.console();
    String guessAsString = console.readLine("Enter a letter: ");
    char guess = guessAsString.charAt(0);
    return mGame.applyGuess(guess);
  }
}

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;
    return isHit;
    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
    return isHit;
  }
}

I would be grateful where I've gone wrong please.

Thanks

Sean

4 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hello,

Would you mind explaining more what you mean by "isn't working"?

One thing I've noticed in your Game.java file is:

public boolean applyGuess(char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    return isHit;
    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
    return isHit;
  }

You are returning your method before it ever gets to the if-else statement. You probably want to remove the first return statement and return only after the if-else statement has run.

Sean Flanagan
Sean Flanagan
33,232 Points

Hi Jon. Thanks for trying to help.

I deleted the first return statement. The second is still where it was.

I tried to run the program again by typing "clear && javac Game.java && java Game" (I didn't type the double quotes :-) ). I got this message:

"Error: Main method not found in class Game, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application"

Then I typed 'Game game = new Game("Treehouse");' and this message appeared in the console:

'Game game = new Game("Treehouse");
-bash: syntax error near unexpected token `(' '

Then "clear && javac Hangman.java && java Hangman" then I got this:

"ERROR: not a statement
clear && javac Hangman.java && java Hangman;
^
ERROR: ';' expected
clear && javac Hangman.java && java Hangman;
^
ERROR: not a statement
clear && javac Hangman.java && java Hangman;
^
ERROR: ';' expected
clear && javac Hangman.java && java Hangman;
^
ERROR: not a statement
clear && javac Hangman.java && java Hangman;
^

ERROR: ';' expected
clear && javac Hangman.java && java Hangman;
^
ERROR: not a statement
clear && javac Hangman.java && java Hangman;
^
ERROR: ';' expected
clear && javac Hangman.java && java Hangman;
^
ERROR: not a statement
clear && javac Hangman.java && java Hangman;
^
"

I don't know whether it's me or the console but I just can't make the program work.

Thanks for your feedback.

Sean

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Sean,

Your code for Hangman.java that you posted is the same as your Prompter class. Could you edit it to what you have there?

Also, when you type "clear && javac Game.java && java Game" I believe you should change each "Game" to "Hangman" since that is where your main (public static void main (String[] args)) method is.

Sean Flanagan
Sean Flanagan
33,232 Points

Hi Jon.

OMG You're right; I've posted the same code twice! Sorry about my mistake.

Okay I'll try again, starting with Hangman:

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 scored a hit!");
        } else {
          System.out.println("Missed!");
        }
        prompter.displayProgress();
    }
}

Next, Prompter:

import java.io.Console;

public class Prompter {
  private Game mGame;

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

  public boolean promptForGuess() {
    Console console = System.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());
  }
}

Thanks for your support and I do beg your forbearance. Hopefully this is more helpful.

Sean :-)

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Hi,

When you are calling the "clear &&... etc" You are doing so "outside" of java-repl, correct?

Sean Flanagan
Sean Flanagan
33,232 Points

Hi Jon.

Yes that's correct.

Sean Flanagan
Sean Flanagan
33,232 Points

Hi again Jon.

Just to let you know that the console is now working.

Your support will be remembered. Thank you :-)

Sean :-)