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

Mariya Betina
PLUS
Mariya Betina
Courses Plus Student 2,026 Points

can not pass the test...

I can not anderstand the Bummer meaning... what is actually incorrect?

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 String getTileCount() {
   String count = "";
    for (char tile: mHand.toCharArray()) {
      if (mHand.indexOf(tile) > 0) {
        count = count + 1;
      }
    } 
    return count;
     }
}
Mariya Betina
Mariya Betina
Courses Plus Student 2,026 Points

Bummer! Did you forget to create getTileCount method that accepts a char?

Simon Coates
Simon Coates
28,694 Points

your code needs to accept a char value to test for. And the if condition looks wrong. The if test just tests that each character in the string is in the string. You want to test tile against a parameter.

1 Answer

Simon Coates
Simon Coates
28,694 Points

The following seems to pass.

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

  }
Mariya Betina
Mariya Betina
Courses Plus Student 2,026 Points

Thanks. That's finally let me pass the challenge. I've got where I was wrong.

Thanks a lot.

Fergus Clare
Fergus Clare
12,120 Points

Excellent analysis and answer Simon. This solved the problem for me as well. FWIW, I don't think the requirements of this challenge are very clear. The request of...

//quote: 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! //

...left me believing that I needed the output of my method to be the numerical value of all of the Chars in the String. After looking at your response, I realized they were focused on the incrementing of the counter IF the tile matches a Char in mHand.