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

Israel Palma
Israel Palma
1,456 Points

I do not understand what is it that getTileCount method should do

Should it count the tiles in the mHand? Or should it count how many repeated tiles does the mHand has?

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 void getTileCount(){

  }

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
}

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points
  // Task 1 of 1 asks:
  // Add a method called 'getTileCount' that ...
  public int getTileCount(char tile) {
    // Initialize count
    int count = 0;
    // uses the for each loop you just learned to loop through the characters
    for (char letter: mHand.toCharArray()) {
      if (letter == tile) {
        // and increment a counter if it matches.
        count += 1;
      }
    }
    // Return the (int) count
    return count;
  }
Israel Palma
Israel Palma
1,456 Points

Hi Chriss so it has to count repeated letters ok. Thank you