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

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 :)