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

java.util.NoSuchElementException No clue why this is happening

Don't what what the problem is here

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

Try adding another bracket } at the end of your code, that worked for me.

5 Answers

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,485 Points

My guess is your scope for multiple variables are wrong.

For this line: public Game(String answer){ mAnswer = answer; mHits = ""; mMisses = ""; } mAnswer & mMisses are not defined in the scope?

Try adding another bracket } at the end of your code, that worked for me.

Alistair Mackay
Alistair Mackay
7,812 Points

Yeah, I struggled with the no "NoSuchElementException" for a while (Still do on occasion).

Basically, it's a typo in your java class. They tend to happen in these exercises because we skip compiling our code before goin into java-repl.

java-repl will load your code regardless of typos so if there is a minor mistake in your code it won't pick up until you try to to create (instantiate) your class in Java-repl.

Matthew Rozmajzl's code:

public class Game { 
  private String mAnswer; 
  private String mHits; 
  private String mMisses;

  public Game(String answer) { 
    mAnswer = answer; 
    mHits = ""; 
    mMisses = ""; 
  }


// There is a typo on the following line: 
  public boolean applyGuess(char letter);
// End of typo
    boolean isHit = mAnswer.indexOf(letter) >= 0; 
    if (isHit) { 
      mHits += letter; 
    } else { 
      mMisses += letter; 
    } 
    return isHit; 
  }
//There should be the end of a bracket between these comments.

//bracket should have gone above.

I messed up because I forgot to include a return statement. If you're really stuck run the below code.

PowerlessCube's Code:

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;
    }
    /* I was missing the return statement as the exercise may have glossed
over this but due to Java's statically typed form this function was
expecting to return a boolean answer.  Returning our 'isHit' fixes the
problem.
*/
    return isHit;
  }
}

java-repl code:

java> :load Game.java
Loaded source file from Game.java
java> Game game = new Game("treehouse");
Game game = Game @6a753a7f // how your initialised class is stored on the computer.
game.applyGuess('t');
java.lang.Boolean res1 = true; // matches one of our letters.
game.applyGuess('z');
java.lang.Boolean res2 = false // doesn't match on our letters.

I hope the above helps you spot these sort of mistakes in future.

I have the same issue after repeating all the steps as mentioned in the video. I think something isn't right.

I have this as the following and am getting an error: 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 = answer.indexOf(letter) >= 0; if(isHit){ mHits += letter; }else { mMisses += letter; } return isHit;

} }

Sachveer Gill, if you change your answer.indexOf into mAnswer.indexOf, then it should work. I hope this helps!

Kendall Rundquist
Kendall Rundquist
9,975 Points

On your applyGuess method you put a semicolon instead of an opening bracket after your parameters:

public boolean applyGuess(char letter); should be public boolean applyGuess(char letter) {