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

Can't return an int.

I am currently trying to pass the ScrabblePlayer challenge where I have to create a getTileCount method, but when I try to return tileCount, I get this

./ScrabblePlayer.java:18: error: incompatible types: int cannot be converted to String return tileCount; ^

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }
  public int tileCount = 0;
  public String getTileCount(char validTile) {
    for (char tile : mHand.toCharArray()) {
      if (tile == validTile) {
        tileCount++;
      }
      return tileCount;
    }
  }

  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;
  }
}

1 Answer

Nicholas Olsen
Nicholas Olsen
1,698 Points
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public int getTileCount(char tile) {// "tile" just for naming purposes. Use "int" since we are returning that
   int tileCount = 0;// declare your int variable and set it equal to 0
    for (char tileInHand : mHand.toCharArray()) {// create new char, the one "we" have
      if (tileInHand == tile) {// if the tile we have is the same as the original
        tileCount++;// increment the tile count, which means we have more than one
      }

    }
      return tileCount;// return the amount of that tile that we have

  }

  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;
  }
}

Pixel HD, I do hope that this helps you! If I made any errors feel free to let me know! You have to define the return type of the method getTileCount() as an int, since that is what we are going to return. Happy coding!

Ohhh, thanks!