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

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,736 Points

Having a hard time with this challenge

Challenge says it wants a method named getTileCount() that accepts a char. But what it seems to want is for me to count the number of tiles in mHand - so I don't understand how I would want a char parameter if that's what they're asking for.

Also I think I have some compile errors, but I'm having a hard time figuring out how to use the workspace to debug this.

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

}

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hey Brendan,

Let me give you this alternative way of wording the challenge.

I'm going to pass in a letter in the getTile method. I want to see how many of that letter is in our mHand string. How would we be able to do that? Using your knowledge of the for-each loop, try to loop through each letter in our mHand string and check if it equals the character that was passed in. Everytime we get a match, increase our counter by 1.

I believe you understand the concept and what you need to do but it was perhaps just the wording that caught you off guard. Post back and let me know if you need help or if you got it working!!

Kevin