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

Sean Flanagan
Sean Flanagan
33,235 Points

Help needed with Scrabble game

Hi.

Can anyone please tell me what's wrong with this:

  public String getTileCount() {
    String letter = "";
    for (char tilesForLetter: mHand.toLetterNumber()) {
      char display = '-';
      if (mHand.indexOf(tilesForLetter) == 0) {
        display = tilesForLetter;
      }
    }
  }

Thanks.

Sean :-)

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public String getTileCount() {
    String letter = "";
    for (char tilesForLetter: mHand.toLetterNumber()) {
      char display = '-';
      if (mHand.indexOf(tilesForLetter) == 0) {
        display = tilesForLetter;
      }
    }
  }

  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

Krasimir Georgiev
Krasimir Georgiev
19,146 Points
//The return type should be int because you're asked to return a count of how many tiles of this type you have.
  public int getTileCount(char tile) {
//The method takes a tile as a paremeter.That is the tile you need to check if you have it.
    int count = 0;
//We initialize the count here.
    for (char tilesForLetter: mHand.toCharArray()) {
//We use a for each loop.
//mHand.toCharArray() changes mHand to a array of characters.
      if (tile == tilesForLetter) {
        count++;
//If the tile which is the parameter is equal to one of the tiles in the hand we increment the count.
      }
    }
  return count;
}