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

Why does this method take the parameter of a char as input, and how is this char used inside the method?

No additional details

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public int getTileCount(char testCharacter) {
    int counter = 0;
    for (char c : mHand.toCharArray()) {
      if (hasTile(c)){
        counter++;
      }
    }
    return counter;
  }

  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;
  }
}

1 Answer

Travis Adams
Travis Adams
3,667 Points

Since you make a call to your hasTile() method, let me first explain this: this method checks to see if the char passed in is present in the mHand variable. The getTileCount() method checks to see HOW MANY of those tiles are present in mHand.

So in the getTileCount() method, the Char testCharacter parameter you pass in will be tested to see how many times it shows up in our mHand. The only changes you need to make is inside your if condition. We don't need to make a call to hasTile, instead, we're checking to see if each letter in the charArray (represented by the variable c) is equal to the parameter we pass in, so your conditional statement will look like this:

if (c == testCharacter)