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) Delivering the MVP Validation

Paul Joiner
Paul Joiner
1,682 Points

Hangman.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!

Paul Joiner
Paul Joiner
1,682 Points
import java.io.Console;

public class Prompter {
  private Game mGame;

  public Prompter(Game game) {
    mGame = game;
  }

  public void play() {
  while (mGame.getRemainingTries() > 0) {
    displayProgress();
    promptForGuess();
    }
  }

  public boolean promptForGuess() {
    Console console = System.console();
    boolean isHit = false;
    boolean isValidGuess = false;
    while (! isValidGuess) {
      String guessAsString = console.readLine("Enter a letter:  ");
      char guess = guessAsString.charAt(0);
      try {
        isHit = mGame.applyGuess(guess);
        isValidGuess = true;  
      } catch (IllegalArgumentException iae) {
      console.printf("%. Please try again.\n", iae.getMessage());
      }  
    }
    return isHit;
  } 

private boolean mGameApplyGuess(char guess) {
        return false;
    }  

 public void displayProgress() {
  System.out.printf("You have %d tries left to solve %s\n", 
                    mGame.getRemainingTries(),
                    mGame.getCurrentProgress());
 } 
}

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hi 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.");
   }  
}

Hi Paul! I think you have just missed s after %;

console.printf("%s. Please try again.\n", iae.getMessage());