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

Pierre-Alexandre GROSS
Pierre-Alexandre GROSS
2,832 Points

Need help for that exercise

I just wrote lines 21 - 29 in the for loop exercise, but the systems keeps asking me if I forgot to create the getTileCount method, but I created it... and there is no compile errors, so I assume that the syntax is correct.

Thanks.

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 () {
    int number = 0;
    for (char letter: mHand.toCharArray()) {
      if (mHand.indexOf(letter) >=0) {
      number ++;
      }
    }
    return number;
  }
}

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hey Pierre,

Your code right now is doing the following: for each of the letters we have right now, match it to the letters that we have, and increase the count. What your doing is basically comparing your mHand to your mHand.

for (char letter: mHand.toCharArray()) {
      if (mHand.indexOf(letter) >=0) {
      number ++;
      }

In case you still cant see it, your looping through all the characters of your mHand in the first line and then comparing it to your mHand in the second line.

What the challenge is asking is that it wants to know the count of a specific tile in your hand. That would mean you have to pass in a char into that method. Therefore your method parameter should look like this:

public int getTileCount (Char tile)

We will call that method and put a letter in it and then check if we have that letter in your mHands. I believe you just read the question wrong as that what happened to me when I first did this. In case you dont know what to do next, I will show you.

So you got everything pretty much correct except lets remove this line:

if (mHand.indexOf(letter) >=0)

We actually dont need this. When we loop through all the characters in our mHands, we just have to check if it matches the letter that we passed in. We can do this simple by using ==

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

The above is the complete answer to the challenge. I hope that made sense and happy coding!

Kevin

Pierre-Alexandre GROSS
Pierre-Alexandre GROSS
2,832 Points

Hey thank you very much, your explanations are very clear. You are right, I might have misunderstood the question. All is alright now, thanks. Pierre.

Kevin Faust
Kevin Faust
15,353 Points

Glad to help :)

Don't forget to mark as best answer, I would also be very thankful