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

where am i wrong? getTileCount

help

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()) {
        if(mHand.indexOf(tile) >-1) {
        count++;
        }
      }
    return count;
   }
}

1 Answer

I had to read this a couple of times. You don't want to count how many tiles the player has - there's no need to use a for:each loop to do that.

The getTileCount method is badly named, in my opinion, but that's irrelevant. The method needs to take a char as a parameter. The for:each loop then iterates through the mHand string and counts how many instances of that char occur. So, if the player's hand was H E L L O, the method would be called like: getTileCount('L'); and it would return 2, the number of Ls in the hand.

My solution looks like:

  public int getTileCount(char tile){
    int count = 0;
    for (char letter : mHand.toCharArray()) {
      if(letter == tile){
        count++;
      }
    }
   return count;
 }

I hope that makes sense!

Steve.

That makes more sense. I think they need to reword the question (because sometimes we don't do these exercises back-to-back and forget things).

I think it should be worded: "So back to that ScrabblePlayer. I found that it's not enough to know if they just have a tile of a specific character. We need to know how many tiles of the same character they actually have. Can you please add a method called getTileCount that uses the for each loop you just learned to loop through the characters and increment a counter if it matches? Return the count, please, thanks!"

Simple change but makes a world of difference. Initially, I read the instructions as though they were asking us to find out how many total tiles the scrabble player has. Thanks for the clarification! Micah

eric Crawford
eric Crawford
2,381 Points

Just brushing up on some review, and this questions wording definitely made me over do it. K.I.S.S. right? Maybe i should tell my self that when things seem over complex.

Yeah, this one catches a lot of people out. Breaking the problem down is a key part of understanding the solution, that's for sure.