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

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

I must not be understanding the challenge question with the For Each loop.

I've gotten all the bugs out of my code. Now it keeps on saying that I need to add a char to it. But every time I change something, it causes far more problems that I started with. Can anyone help me please. I'm struggling pretty badly here.

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 (char tile : mHand.toCharArray()) {
      int counter = 1;
      if (mHand.indexOf(tile) >= 0) {
       counter = tile;
      }
      count += counter;
    }
    return count;
  }
}

1 Answer

I'm assuming getTileCount() is intended to obtain the number of tiles (letters) the player has in their hand that match the tile parameter provided. A couple of issues I see: (1) You are passing in a char "tile" parameter and using the same "tile" local variable in your for loop. Use a differently named variable in your loop (e.g., handTile) to avoid confusion. (2) the statement "counter = tile" is setting an integer "counter" equal to the value of the letter, which doesn't make sense. Suggest removing "counter" entirely. You're almost there. You want to compare the tile parameter passed in with each tile char in mHand.toCharArray() and increment "count" when they match (are equal). When the loop finishes you should have the number of tiles in the hand matching the tile parameter. ( if(tile == handTile) count++; inside the loop )

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

Thank you so much. I have a lot of learning to do for sure. I was trying to do as much as I could on my own and I got a little bit done. I had even searched through the Oracle documents for the hopes that I could learn something that I had missed. I just need to think a bit more when doing it. I ended up changing the code and it works great. It even looks more clean than I had it originally looking...

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

That code worked just fine. I also thank you for not just writing the proper code in there and explaining to me what I had done wrong and giving me the opportunity to learn from my mistakes and be guided in the right direction. Happy Coding :)