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 Remaining Tries

[SOLVED] What happens at this stage when you successfully guess the word?

I get a syntax error, for the loop exits only on a failure.

You have 3 tries left to solve: treehouse
Enter a letter:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:646)
at Prompter.promptForGuess(Prompter.java:20)
at Prompter.play(Prompter.java:13)
at Hangman.main(Hangman.java:7)

Ricky Libby
Ricky Libby
1,664 Points

While I'm not able to see your code, I'm going to take a crack at this...

It looks like the string you are using hasn't yet been initialized and in the following line I would imagine you are trying to use it?

Try initializing your string with a value:

String myString = "";

1 Answer

It turns out that this error occurs in the next lesson and is addressed in the lesson after that:

When reading from console we can get a zero length string. So,

"Zero Length String".charAt(0);

Returns an unhandled exception. This was handled by checking the length of string when reading from console and raising a known exception if it is of zero length.

public boolean applyGuess(String letters) {

if (letters.length() == 0) {

throw new IllegalArgumentsException("No letter(s) found!");

}

char firstLetter = letters.charAt(0);

return applyGuess(firstLetter);

}