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

Elisabeth Frost
Elisabeth Frost
959 Points

java.util.NoSuchElementException

I have looked and looked at my code and tried looking at other questions about this but I can't understand why I keep getting this error. Any help is much appreciated. I don't want to move ahead in the course until I understand what's happening.

Here's what I have in each file.

Hangman.java file:

public class Hangman {

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

}

Prompter.java file:

public class Prompter { }

Game.java file:

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

3 Answers

Where are you running the code?

I copy-pasted your code and compiled and ran it without getting the java.util.NoSuchElementException:

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 class Hangman {

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

}
public class Prompter {
}
Elisabeth Frost
Elisabeth Frost
959 Points

Weird. I'm running it in Workspaces.

I uploaded your code onto one of my workspaces https://teamtreehouse.com/workspaces/15935202

use "javac Hangman.java" to compile the code. and "java Hangman" to run it.

The program doesn't really output anything, but it works fine.

Even though I had no errors, I was able to get it working in the repl after: a.) Getting out of repl (CTRL + C) b.) Compiling it (javac Hangman.java) c.) Getting back into the repl and following the steps again from load

Sometimes workspaces is a little wonky.