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

Michael Lee
Michael Lee
2,541 Points

I was able to figure out how to return the tile count, but I am wondering if there is better way to do this?

I set a variable of "letter" to see if it will match the input for the getTileCount method.

Also, I am wondering about the repeated usage of 'tile' as a variable for the different methods. Every instance of 'tile' is distinguishable between the methods?

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()) {
      if(letter == tile) {
      count ++;
    return count;

1 Answer

Dan Johnson
Dan Johnson
40,533 Points

Your solution is good, it's simple and clear like methods should be. And since you're dealing with characters in an arbitrary order there's nothing to really worry about with optimization.

As for the reuse of names for arguments or variables local to methods, you don't need to worry about. They are tied to the scope of that specific method and will only exist during the duration of a call to that method.

If you had a property with the same name however, the local variable will "shadow" the property. You can deal with the ambiguity using the this keyword.

public class MyClass {
    private int sameName;

    public MyClass(int sameName) {
         // this.sameName refers to the property
         // sameName refers to the argument.
         this.sameName = sameName;