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 For Each Loop

./ScrabblePlayer.java:29: error: incompatible types: int cannot be converted to String return count;

hello guys..!! public String getTileCount(char tile){ int count = 0; for(int i =0;i<mHand,length(); i++){ if(mHand.charAt(i) == tile){ count++; } //I placed here too and give me same error. } return count; }

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public void addTile(char tile) {
    // Adds the tile to the hand of the player
    mHand += tile;
  }

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
  public String getTileCount(char tile) {
    int count = 0;
    for(int i = 0; i < mHand.length(); i++){
    if(mHand.charAt(i) == tile){
    count ++;
    }
    }
    return count;
  }
}

4 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

You've set the return type of the getTileCount() method to String, but it should be int.

thx Kourosh Raeen and Grigorij. have a question ? why did you place "return count " out of the loop instead of put it inside of the loop?

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Fernando, sorry for confusion. The placement of the return statement of your code was 100% correct.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

HI Fernando,

i have changed your method a little bit:

public int getTileCount(char tile){ // return type is int not String
    int count = 0;
    for(int i = 0; i < mHand.length() ;i++){
      if(mHand.charAt(i) == tile)
        count++;
    }
  }
return count; 
}

Makes sense?

Grigorij

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Grigorij - Sorry to be picky but I need to make a correction. Fernando's return statement was already inside getTileCount. The two closing braces above his return statement are for the if and for statements.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Kouroush,

you are not picky at all!!! Thank you for the correction. I changed it after I saw my mestake :)

You are great :thumbsup:

Grigorij

thx dude..!!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

You are welcome !!!

And thx to Kourosh for the correction :)