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

Steven Hall
Steven Hall
841 Points

PLEASE help

i have no idea what the question is trying to ask.. why would a counter method accept a char?? \is it not asking to increase a counter for every tile? the question also says " increase a counter if it matches" ??? if what matches lol again is it asking to increase a counter for every tile in the hand or.....

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 int getTileCount () {
    int count = 0;
    for (char tile: mHand.toCharArray()) {
     count += 1; 
    }
    return count;
  }

}

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

You are trying to find out the count for a specific tile in a hand so your method should accept a parameter of type char for the tile. Also, you need to increment count only if a character in mHand matches the given tile:

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 int getTileCount (char tile) {
    int count = 0;
    for (char character: mHand.toCharArray()) {
      if (character == tile) {
        count++; 
      }
    }
    return count;
  }
}