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

Getting Error

I keep getting this error after I've entered the play method. I've gone back through the video but not sure what i'm doing wrong. Its the Java Objects Remaining Tries video I'm at. Sorry for the long code pasting here.

https://teamtreehouse.com/library/java-objects/creating-the-mvp/remaining-tries

Error Message:

You have Exception in thread "main" java.util.IllegalFormatConve rsionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Fo rmatter.java:4302)
at java.util.Formatter$FormatSpecifier.printInteger(Form atter.java:2793)
at java.util.Formatter$FormatSpecifier.print(Formatter.j ava:2747)
at java.util.Formatter.format(Formatter.java:2520)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Prompter.displayProgress(Prompter.java:26)
at Prompter.play(Prompter.java:13)
at Hangman.main(Hangman.java:7)

My code... (Prompter.java)

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();
    String guessAsString = console.readLine("Enter a letter:  ");
    char guess = guessAsString.charAt(0);
    return mGame.applyGuess(guess);
  }

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

}

(game.java)

public class Game {
  public static final int MAX_MISSES = 7; 
    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 String getCurrentProgress() {
    String progress = "";
    for (char letter: mAnswer.toCharArray()) {
      char display = '-'; 
      if (mHits.indexOf(letter) >=0) {
        display = letter; 
      }
          progress += display; 
    }
    return progress; 
  }

  public int getRemainingTries() {
    return MAX_MISSES - mMisses.length();
  }
}

(Hangman.java)

public class Hangman {

    public static void main(String[] args) {
        //Enter code here:
        Game game = new Game("treehouse");
    Prompter prompter = new Prompter(game);
    prompter.play();
    }
}

I'm not sure if this is the silver bullet, but one thing that stands out is:

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

You have mGame.getCurrentProgress() called twice.

Thanks Yonatan that is one of my problems I think, I caught that one right before you posted

1 Answer

Hey Helen,

the print formatted method takes a format string, with format specifiers like '%d' and '%s' in it, and a number of arguments which will be formatted according to the specifiers and then replace them within the format string.

When using the 'd' conversion in the '%d' format specifier, the Formatter is expecting an integral to convert it into a decimal integer. The 'mGame.getCurrentProgress()' method is returning a String, though. That's the reason you are getting the 'IllegalFormatConversionException'. It cannot format a String lik "He--n" into a decimal integer.

All you gotta do is replace the first 'getCurrentProgress()' method with the 'getRemainingTries()' method . The latter returning an Integer.

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

You can find more details on conversions here: Java Formatter

Regards, Florian