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

}