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

FOR EACH LOOP

public class ScrabblePlayer { private String mHand;

public ScrabblePlayer() { mHand = ""; }

public String getHand() { return mHand; }

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

}

return count;
}

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

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

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

    }
  return count;  
  }

  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

Zachary Lilley
Zachary Lilley
4,288 Points

Hey Ashford,

I was also stuck on this problem last night and figured it out after giving it a break and coming back this morning. Looking at your code, you need to read in a char paramater so the code will know which tile to look for. Also, you need to create an if statement inside your For Loop in order for it to check if the char you read in as a paramater matches the char of the tile and then if it does, increment your count variable. As of now, your code is incrementing your count variable for every tile no matter the char on it. Other than that, your code looks identical to mine. If you need further assistance let me know and I can publish my answer.

Good luck!

can you please publish your answer because i have tried to create the character variable that accepts the added character but its not working maybe if i see your solution i'll figure it out.

Zachary Lilley
Zachary Lilley
4,288 Points
 public int getTileCount(char tile) {
    int count=0;
    for (char letters : mHand.toCharArray()) {
        if ( letters == tile) {
              count++;
      }
   }
return count;
}

thank you !!!