Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP For Each Loop

creating the for each loop

i am trying to figure out what i use to help getTileCount to read how many tiles there are to increment the count and how i could use the if statement to compare the words

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(){
      count++;
    }
  }
}

Can you explain your methods a bit more? What tiles does getTileCount count exactly? The number of tiles in hand or on the board or what?

1 Answer

So what should you have in your for parenthesis? Let's see what you want - For each character in the String, do something. However, you cannot directly do that, so try using the toCharArray() method you learned about in the previous video.

Now the task wants you to check if the hand has a specific character. So you need to check that. Simply add an if statement inside the foreach loop and check if the passed char (as parameter for the function) is the same as the current one from the mHand variable. If they are the same character, increment the counter.

Try figuring the code on your own. If you have any issues, make sure to ask again :)