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 trialPaul Joiner
1,682 PointsHangman.java crashes when I enter invalid/already used answers. I've followed the video example. What's wrong?
Hangman.java compiles with no apparent errors, however when I go to enter an "invalid" answer such as a number, or previously guessed letter, I get an error message reading :
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '.'
at java.util.Formatter.checkText(Formatter.java:2579)
at java.util.Formatter.parse(Formatter.java:2565)
at java.util.Formatter.format(Formatter.java:2501)
at java.util.Formatter.format(Formatter.java:2455)
at java.io.Console.format(Console.java:170)
at java.io.Console.printf(Console.java:209)
at Prompter.promptForGuess(Prompter.java:28)
at Prompter.play(Prompter.java:13)
at Hangman.main(Hangman.java:7)
What's going on? I already added a private mGameApplyGuess method as it had been suggested in multiple threads (even though I didn't see it in the video example). My prompter code is posted below. Thanks!
2 Answers
Damien Watson
27,419 PointsHi Paul,
Not sure if this is it, but you are doing string manipulation outside the try. If it is invalid, it is not being caught. Also, I'd remove the IllegalArgumentException message as the user only needs something simple.
while (! isValidGuess) {
try {
String guessAsString = console.readLine("Enter a letter: ");
char guess = guessAsString.charAt(0);
isHit = mGame.applyGuess(guess);
isValidGuess = true;
} catch (IllegalArgumentException iae) {
console.printf("Invalid entry, please try again.");
}
}
Sahil Nabizada
15,696 PointsHi Paul! I think you have just missed s after %;
console.printf("%s. Please try again.\n", iae.getMessage());
Paul Joiner
1,682 PointsPaul Joiner
1,682 Points