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 have no idea whats wrong, java experts please help!

I am following a video tutorial from treehouse and i thought i was writing the exact same thing as the teacher, but apparently not. When i compile the code i get 13 errors, i am trying to fix the problems but i don't understand some of the errors. Here are my codes:

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());
  }
}
Hangman.java
public class Hangman {

    public static void main(String[] args) {
        // Enter amazing code here:
      Game game = new Game("treehouse");
      Prompter propmter = new Prompter(game);
      propmter.displayProgress();
      boolean isHit = prompter.prompterForGuess();
      if (isHit) {
        System.out.println("We got a hit!");
      } else {
        System.out.println("Whoops that was a miss");
      }
      prompter.displayProgress();
    }
}

Console:

treehouse:~/workspace$ javac Hangman.java
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
./Game.java:27: error: illegal start of expression
if (mHits.indexOf(letter)) >= 0 {
^
./Game.java:27: error: ';' expected
if (mHits.indexOf(letter)) >= 0 {
^
./Prompter.java:4: error: ';' expected
Private Game mGame;
^
./Prompter.java:4: error: <identifier> expected
Private Game mGame;
^
./Prompter.java:4: error: cannot find symbol
Private Game mGame;
^
symbol: class Private
location: class Prompter
./Prompter.java:4: error: cannot find symbol
Private Game mGame;
^
symbol: class mGame
location: class Prompter
Hangman.java:8: error: cannot find symbol
boolean isHit = prompter.prompterForGuess();
^
symbol: variable prompter
location: class Hangman
Hangman.java:14: error: cannot find symbol
prompter.displayProgress();
^
symbol: variable prompter
location: class Hangman
./Game.java:27: error: incompatible types: int cannot be converted to boolean
if (mHits.indexOf(letter)) >= 0 {
^
./Prompter.java:7: error: cannot find symbol
mGame = game;
^
symbol: variable mGame
location: class Prompter
./Prompter.java:12: error: incompatible types: String cannot be converted to boolean
String guessAsString = console.readline("Enter a letter: ");
^
./Prompter.java:14: error: cannot find symbol
return mGame.applyGuess(guess);
^
symbol: variable mGame
location: class Prompter
./Prompter.java:18: error: cannot find symbol
System.out.printf("Try to solve: %s\n", mGame.getCurrentProgress());
^
symbol: variable mGame
location: class Prompter
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 13 errors

Have you done it via the Workspaces? Then you could share your workspace with the community. Just create a snapshot (one of the 3 buttons on the right top corner) post the link here and we can take a look at your workspace.

3 Answers

Hi Mehrdad,

A few points based on what you've posted. Maybe try fixing these and seeing what problems still exist. We can then fix those.

Here, the test needs to be inside the brackets:

if (mHits.indexOf(letter)) >= 0  { //change this to

if (mHits.indexOf(letter) >= 0) { // >= inside the ()

Next, private is a lowercase initial letter:

Private Game mGame; // change this to 
private Game mGame;  // <- this

This needs a captial L in readLine():

String guessAsString = console.readline("Enter a letter: "); // change to

String guessAsString = console.readLine("Enter a letter: "); // capital L inside readLine

Try fixing those and let me know how you get on - there may still be errors, but that should thin them out a bit. You can also share the Workspace as Tobias Krause suggested. :+1:

Steve.

Thanks! I changed what you said and now I am only getting two errors.

treehouse:~/workspace$ javac Hangman.java
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Hangman.java:8: error: cannot find symbol
boolean isHit = prompter.prompterForGuess();
^
symbol: variable prompter
location: class Hangman
Hangman.java:14: error: cannot find symbol
prompter.displayProgress();
^
symbol: variable prompter
location: class Hangman
2 errors

Apologies - I should have seen this!

Your error is bcause your code doesn't know what prompter is; that's what a "cannot find symbol" error is.

The reason for this is because you've typod it when you declared it inside Hangman.java. Be aware you have also used the wrongly spelled instance too:

Prompter propmter = new Prompter(game);
propmter.displayProgress(); 

// change to ... 

Prompter prompter = new Prompter(game);
prompter.displayProgress();

That should sort the last errors out for you. :smile:

Steve. :+1:

Yes that did fix the previous errors, i still have 1 error which is this:

treehouse:~/workspace$ javac Hangman.java
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Hangman.java:8: error: cannot find symbol
boolean isHit = prompter.prompterForGuess();
^
symbol: method prompterForGuess()
location: variable prompter of type Prompter
1 error

it does not look like i made a spelling error but i might be wrong.

Yes, you've called the method the wrong thing. THe method name is promptForGuess(), not prompterForGuess().

Change:

boolean isHit = prompter.prompterForGuess();

to:

boolean isHit = prompter.promptForGuess();

Steve.

Thanks! Silly mistake of me. Need to be more careful and make sure i type the right thing.

:+1: