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

Jonathan Weinstein
Jonathan Weinstein
9,063 Points

Not sure what argument to pass into function here.

The prompt calls for a method, which I've added at the end, that can accept a char. I'm trying to pass in "tile" but am getting error messages. Anyone know why?

public class ScrabblePlayer {
  private String mHand;
  private int tileCount;


  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 void getTileCount(tile) {
    for (char tile: mHand.toCharArray()) {
      if (mHand.indexOf(tile) >=0) {
      tileCount += 1;
      }
    }
  }
}
Jonathan Weinstein
Jonathan Weinstein
9,063 Points

Also not sure where to initialize the variable tileCount. Would appreciate an explanation of how to do this!

1 Answer

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

Hello, you're close on this, just a few minor errors, first off, I'd name your char in if for statement different than the variable that is expected in the method signatures, this just makes things less confusing, so instead something like for(char letter: mHand.toCharArray())

Also, if your if statement see if that variable you created in your the above for statement is equal to the character that the origional method had passed in. so in the chase of letter it would look something like If (letter == tile).

You actually initialized the variable in the correct spot, it looks like you just called the variable a name that was already taken, and that could have confused your code.

It should look something like this.

public int getTileCount(char tile){
    int count = 0;
    for(char letter : mHand.toCharArray()){

      if(letter == tile){
        count++;
      }
    }
    return count;
  }
}

Looks like you were slightly over thinking this one, I do the same often.

Thanks, let me know if this helps or not and I'll give it another look.