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

I am getting no compiler errors, but my code doesn't pass?

It says that I didn't creat the getTileCount method that accepts a char.

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public int getTileCount() {
    int Count = 0;
    for (char tile: mHand.toCharArray()) {
       Count = Count + 1;
    }
    return Count;
  }

  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;
  }
}

2 Answers

Michael Hess
Michael Hess
24,512 Points

Hi Spencer,

It looks like you're really close! But, try adding a char tile argument to getTileCount() and an if-statement, inside the for-each loop, to check if the letter matches the tile, then add 1 to the count.

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

If you have any other questions feel free to ask! Hope this helps!

Michael Norman
PLUS
Michael Norman
Courses Plus Student 9,399 Points

The goal of this challenge is that getTileCount will be passed a character. You want to check how many occurrences of that character are in you hand.

// the character you are checking for is passed in
public int getTileCount(char tileToCheck) {
    int Count = 0;
    for (char tile: mHand.toCharArray()) {
        // TODO: if tile is equal to the tileToCheck parameter, increase count
       Count = Count + 1;
    }
    return Count;
  }