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 Java Objects (Retired) Creating the MVP Current Progress

Sean Flanagan
Sean Flanagan
33,235 Points

Am I doing this right?

Hi. I can't get the code to work in the console.

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

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;
  }
}

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);
  }

  public void displayProgress() {
    System.out.printf("Try to solve: %s\n", mGame.getCurrentProgress());
  }
}

In the console I typed: java-repl. Then: clear && javac Hangman.java && java Hangman. The program wouldn't run. Here's what I got:

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'd appreciate any help.

Sean

1 Answer

Exit from the repl and write that line again.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi. Thanks for trying to help.

I've just got home from work. I typed in, without using the repl:

clear && javac hangman.java && java hangman

This time I got this message:

use -help for a list of possible options

Have I done this right?

Sean

capital "H"