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

fahad lashari
fahad lashari
6,771 Points

java.utilNoSuchElementException

import java.lang.*; 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 = false;

if (mAnswer.contains(letter)){
  isHit = true;
}
return isHit;

} }

Have no idea what I am doing wrong. I am simply trying to use a different method for achieving this task, however in rep, every time I try to create a new object for Game.java I get the 'java.utilNoSuchElementException' error. Can someone help me out please. Would really appreciate it.

4 Answers

fahad lashari
fahad lashari
6,771 Points

Hi all, I actually found the bug in my code that was causing the problem, it the fact that in the method 'applyGuess' there is an 'if' statement which concatenates the mHits with the char letters if the isHit variable is true. I accident did:

if (isHit){ isHit += letter; }

the Right way was:

if (isHit){ mHits += letter; }

Hope this helps someone.

Re-check your code for any misspelling. This can cause the 'java.utilNoSuchElementException' error.

Kind regards

Maciej Torbus
Maciej Torbus
8,137 Points

I'm very happy that you found your mistake and also re-changed the applyGuess method. Don't worry about the spelling mistake though, java-repl skips compiling the code, but in any IDE that you will be using in future, mistakes like this will be found easily. The main thing here is to understand the code, little typos are everyday life in programming! ;)

Maciej Torbus
Maciej Torbus
8,137 Points

Hello Fahad,

I'm not sure about creating the object but I found a mistake in your applyGuess method.

You are using method contains() which takes CharSequence as a parameter. You first need to change your letter which is a char into String for example, which is a sequence of chars.

You can do that using toString() method of Character class, like this:

          Character.toString(letter)

and you can pass it then to contains method.

Or you can do that other way (like Craig in the video) using indexOf which accepts char. It returns the index where character occured or -1 otherwise.

Please take that in notice and I'm sorry that I was unable to help you with creating a Game object. You gave us code of the game class and I was able to check that out, could you please post also the part of code where you are actually creating the object?

Hope I could help you a little bit!

fahad lashari
fahad lashari
6,771 Points

Thank you for your help! I will post the rest of the code when I get back on my PC.

Regards

fahad lashari
fahad lashari
6,771 Points

Here is code for the Game class:

```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 = false; boolean isHit = mAnswer.indexOf(letter) >= 0; if (isHit){ isHit += letter; } else { mMisses += letter; } return isHit; } }```

Here I am trying to create a new object for Game.java in the repl:

```Welcome to JavaREPL version 303 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_65)
Type expression to evaluate, :help for more options or press tab to auto-complete.
java> :load Game.java
Loaded source file from Game.java
java> Game game = new Game("fahad");
java.util.NoSuchElementException