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

Abe Daniels
PLUS
Abe Daniels
Courses Plus Student 2,781 Points

What does this question mean?

I don't know what the question means by "Increment a counter if it matches." I don't know why I need to increment, what a counter is, or what it is matching to.

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 getTileCount() {
    for (char tile: hasTile.toCharArray()) {
      //increment counter if it matches.
    }
    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

Hey there, what the question is basically asking you to do is create a method that accepts a Char as an argument, and in the body of the method check and see if that character is contained in the player's hand.

We can do this by.

public int getTileCount(char tile) {
    int counter = 0; // we initalize our int counter here
    for (char letter : mHand.toCharArray()) {
      if (tile == letter) { // check to see if the the char we're currently check matches the tile we are looking for
         counter++; // increment counter, which is now one
      }
    }
  return counter; // return a int representing how many times the letter that we searched for was found 
}

Should do the trick. Let me know if there's anything else I can further clarify.