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

Adrian Sanchez
Adrian Sanchez
2,131 Points

What's the solution to this objective?

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

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;
  }
}
Adrian Sanchez
Adrian Sanchez
2,131 Points

"So back to that ScrabblePlayer. I found that it's not enough to know if they just have a tile of a specific character. We need to know how many they actually have. Can you please add a method called getTileCount that uses the for each loop you just learned to loop through the characters and increment a counter if it matches? Return the count, please, thanks!"

6 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

You can use the toCharArray() method of the String class to turn mHand into an array of chars so you can loop through them. Then in each iteration of the loop use an if statement to compare the current character in the array with tile. If they are the same increment count, which you have defined as an int and initialized to zero outside the for each loop. Finally, return count.

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Adrian! Here's the complete code, just in case you're still having difficulty with it:

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 (char c: mHand.toCharArray()) {
      if (c == tile) {
        count++;  
      }
    }
    return count;
  }
}
Adrian Sanchez
Adrian Sanchez
2,131 Points

Can you show me an example please? I still can't figure it out.

Kourosh Raeen
Kourosh Raeen
23,733 Points

The for each loop looks like this:

for (char c: mHand.toCharArray()) {
    if ( ) {

    }
}

See if you can figure out the if statement. Let me know if you need more help.

Adrian Sanchez
Adrian Sanchez
2,131 Points

public String getTileCount() { String count = ""; for (char c: mHand.toCharArray()) { if (mHand.indexOf(tile) >= 0) { } } return count; }

This is what i have.

Kourosh Raeen
Kourosh Raeen
23,733 Points

count should be of type int so that you can increment its value. The if statement should check to see if c is equal to tile. If they are equal then do count++.

Adrian Sanchez
Adrian Sanchez
2,131 Points

Thank you! You were very helpful.