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

Ray Wai
Ray Wai
2,355 Points

Cannot solve this task

I cannot seem to grasp what is it the system wants me to solve, it just kept printing out "Did you forget to create the method getTileCount that accepts a char?" What is wrong with my getTileCount method?

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

1 Answer

Hi Ray,

The code has to accept a char as input on the function call, and then it has to check how often the char occurs in the mHand String. Your code didn't accept any input, and it only checked if the char tile from the String mHand is included in the String mHand, which doesn't make sense. This is the correct solution:

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

Happy Learning!

Philip

Ray Wai
Ray Wai
2,355 Points

Philip,

Thanks for the swift reply. Now that looking back at my original code, that mHand.indexOf(tile) >= 0 does make no sense in this situation at all.

Ray

Glad to help :)