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 Storing Guesses

Sean Flanagan
Sean Flanagan
33,235 Points

Console is playing up again

Hi. When I typed in

Game game = new Game("treehouse");

I got this error message on the console:

java.util.NoSuchElementException

Here's the code for my Java programs, starting with 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;
    }
  }
}

Next, Prompter.java:

public class Prompter {

}

Lastly, Hangman.java:

public class Hangman {

    public static void main(String[] args) {
        // Enter amazing code here:
        Game game = new Game("treehouse");
    }

}

I did type :load Game.java first.

I would appreciate any help please.

Thanks

Sean :-)

2 Answers

Sam Parkash
Sam Parkash
6,645 Points

Hi Sean, Try erasing the line from your main method Game game = new Game("treehouse"); It worked for me. Good luck I hope that works for you.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Sam. The game works now. Thanks. :-)

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

I have modified this part of your code. Maybe this could work, but I am not sure.

 public boolean applyGuess(char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
  return isHit;
  } 
Sean Flanagan
Sean Flanagan
33,235 Points

Hi Grigorij. I tried your code and got another error message on the line:

if (isHit) {

I'd be grateful for any other ideas.

Thanks

Sean :-)

Sean Flanagan
Sean Flanagan
33,235 Points

I typed:

Game game = new Game("Treehouse");

I got:

"-bash: syntax error near unexpected token `(' "

Cheers