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

Alex Rodriguez
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Rodriguez
Front End Web Development Techdegree Student 20,810 Points

How do u get getTileCount to accept char?

I have been trying to solve this challenge, I thought the method was accepting char since it was declared in for ( char tile: mHand.toCharArray()) Otherwise what is that doing?

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 String getTileCount() {
   String counter = "";
    for ( char tile: mHand.toCharArray()){
      char display = '-';
      if (mHand.indexOf(tile) >= 0 ) {
       display = tile;
      }
     counter += tile; 

    }
    return counter;
  }
}

1 Answer

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Alex,

I think I just answered the earlier question about this, but what the for loop is doing is looping through each character in your mHand.toCharArray(), which basically, it is breaking apart each letter or character in the string and then running the code in the loop beneath it.

Thanks.

I'm not sure if this is what you were meaning with your question, but to make getTileCount accept a character as an argument, we'd pass that as a parameter into it.

getTileCount(char letter)

Or something similar.