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

What's truely needed here?i didn't understood how to solved this probs..

Any question more specifically.. Thanks for help

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

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 Ricky,

So what this challenge is asking you to do is create a new method that takes an argument of a character, what you want the method to do is loop through all tiles in your hand and see if it matches the character that we're looking for, we'll need to create an int to track the count of number of tiles, incase someone has more than one.

So to break it down

1: create a method called getTileCount, have it accept the a char as a parameter

public int getTileCount(char tile)

Should do the trick.

2: set an int variable to 0, to track the number of tiles this tile appears in your hand, we can do this with a for loop.

int count = 0;

Fairly straight forward part.

3: we then create a loop to go through the hand compare the tiles in the hand to the tile that we want to check for.

for (char letter : mHand.toCharArray()) {

    }

This is where we start to venture in new water, basically what this loop is doing is going through each character in mHand.

4: increment count if it finds a match

We combine the increment statement in an if statement inside the loop, it should look like

 for (char letter : mHand.toCharArray()) {
      if (tile == letter) {
        count++;
      }
    }

5: Finally, return the count:

It should look something like this below

public int getTileCount(char tile) {
    int count = 0;
    for (char letter : mHand.toCharArray()) {
      if (tile == letter) {
        count++;
      }
    }
    return count;
  }

Thanks, let me know if this hasn't helped you, and I'll see what else I can do.