Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP For Each Loop

Raiyan Rahman
PLUS
Raiyan Rahman
Courses Plus Student 8,091 Points

Java Objects: Answer is not being accepted for Scrabble tile count code challenge.

Hello everyone, I've been trying to create a method that will give me a count of the number of times a tile appears in a given hand.

I believe the code if I've written is the correct answer to the question. However, the compiler is not accepting my answer.

Could some give me some pointers on where I could be going wrong?

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 each loop to count instances of tile.
    for(char tileInHand : mHand.toCharArray()) {
      if(tileInHand == tile) {
        count += 1;
      }
    }
    return count;
  }

1 Answer

Jack Middlebrook
Jack Middlebrook
19,745 Points

It looks like your problem is your public int getTileCount(char tile) is outside of the bracket that closes your scrabble player class. If you move the following code inside the last } you should be good:

public int getTileCount(char tile) {
    int count = 0;
    //For each loop to count instances of tile.
    for(char tileInHand : mHand.toCharArray()) {
      if(tileInHand == tile) {
        count += 1;
      }
    }
    return count;
  }
Raiyan Rahman
Raiyan Rahman
Courses Plus Student 8,091 Points

Oh now I see; I can't believe I missed that.

Thank you so much for your help.