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

Marcus Yow
Marcus Yow
3,586 Points

Don't know what I am doing wrong on getTileCount challenge and I'm out of ideas.

What am I over looking.

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

1 Answer

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello, you're close on this one! It just needs a few tweeks, first off your count += 0; isn't adding much to the code, you can leave it there if you want but so to make the code cleaner I'd recommend taking it out.

Then really the only problem with your code was your if statement.

You should adjust it so that it checks if the char letter that you created in your for loop is equal to the tile you are having the method check for.

It should look something like this.

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

      if(letter == tile){
        count++;
      }
    }
    return count;
  }
}

Overall, it looks like you were just over thinking the problem, and pretty much had it yourself.

Thanks, let me know if this helps or not and I'll give it another look.

Marcus Yow
Marcus Yow
3,586 Points

Thanks a lot brother I tend to over complicate things most of the time. But your solution worked.

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

No worries,

I wouldn't be entirely truthful if I said the same thing never happened to me. I usually end up rolling my eyes at myself on how if I would have just trusted my gut on something I could have had the answer 20 minutes ago, guess it's part of the learning experience for everyone.