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

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 :)