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

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.