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

Jason Holt
Jason Holt
10,041 Points

Cannot Complete getTileCount with information given

Struggling with getTileCount. I thought I understood what he was saying but I have been trying different things for thirty minutes and keep getting two main errors: 'returned 8 instead of 1' and 'did you create a getTileCount method that takes a char?' It doesn't say anywhere in the instructions that getTileCount needs to take a char. Putting that information in the parameters throws me off completely. Based on the Hangman app model I don't know where to go with this. Super frustrated.

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

}

1 Answer

It takes a "char" because that is the type of tile.

Try something like this:

  public int getTileCount(char tile) {
    int count = 0;
    String newHand = mHand;
    for (char letter: newHand.toCharArray()) {
      if(letter == tile) count += 1;
    } return count;
  }
Jason Holt
Jason Holt
10,041 Points

Well thank you for your help. I feel like I have a few holes in my understanding that shouldn't be there. This seems pretty abstract without seeing the rest of the application. Is that normal?

You are welcome. This is pretty much the application so far. What part are you having a hard time with?

And yes, it is completely normal to be confused. I am all the time and unfortunately I don't use Java that often and confused somebody else on this same question about a month ago.