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) Delivering the MVP Determining if It Is Solved

Brendan Milton
Brendan Milton
9,050 Points

Incompatible types error - im so confused.

Im Having trouble getting past this part in my code contained in the prompter class

any ideas where ive gone wrong?

this is the error:

public void addItem(Product item, int quantity) {
    System.out.printf("Adding %d of %s to the cart.%n", quantity, item.getName());
    /* Other code omitted for clarity */
  }
public void addItem(Product addedItem) {
addItem(addedItem, 1);
}

my code is below:

import java.io.Console; // clas were importing from java IO

public class Prompter{
  private Game mGame; //member variable 

  public Prompter(Game game){
    mGame = game;
  }
    //example.charAt(0); 

  public void play(){ // limit scope to the story were working on
    while(mGame.getRemainingTries() > 0) {
      displayProgress();
      promptForGuess();
    }
  } 

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

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

Is your applyGuess method written to convert the String into a char?

3 Answers

Brendan Milton
Brendan Milton
9,050 Points

incompatible types error 25:

and its focusing on the below line of code

isHit = mGame.applyGuess(guessAsString); ^

Why did you do that Nicolas Hampton? The code should look like:

isHit = new mGame.applyGuess(guessAsString); 

Your code would give errors if you put that in it.

When you declare and initialize your console, you have to add "new" to the code. Other than that, it's really hard to read what's going on there, next time surround your code with "java" and "" so it comes up in a black code box for clarity. Anyway, the declaration should look like this

Console console = new System.console();