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

RaJohn Butler
RaJohn Butler
2,277 Points

Not sure where I am messing up.

So I am trying to use the hasTile method for the tile comparison. But not sure where I am missing something. I feel like I understand what I am being asked to do.

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 int getTitleCount(char tile)
  { 
    int titleCount=0;
    for(char letter : mHand.toCharArray())
    {
      if(mHand.hasTile(tile))
      {
        tileCount++;
      }
    }
    return tileCount;
  }


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

1 Answer

Nicholas Olsen
Nicholas Olsen
1,698 Points

You are so close!! I was confused when I first attempted this one also! If we need to check and see if the tile we have is equal to the other tile, what can we use?

if (letter == tile) {// if the tile we have is the same as the original
        tileCount++;// increment the tile count, which means we have more than one
      }

Also, you defined your method as "getTitleCount", I always put the "t" in tile too for some reason :D same thing for your int variable. Just a pesky little "t" messes everything up! Happy Coding, and great Job! :)

RaJohn Butler
RaJohn Butler
2,277 Points

Thank you so much Nicholas that did the trick!!!!!