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

Emeka Okoro
PLUS
Emeka Okoro
Courses Plus Student 11,724 Points

for each loop

It says that the hand was "sreclhak", 'e' was checked , expected 1 but got 8. cant seem to find a solution

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



}
Dennis Addo
Dennis Addo
Courses Plus Student 2,943 Points

Inside the foreach loop you don't need indexOf again but you already have everything in charArray. if (tile == tile1) {
count++; }

2 Answers

One small error. You have this:

if (mHand.indexOf(tile1) >= 0) { 

This checks to see if tile1 is in the hand. What you need instead is to see if the current tile equals the passed in one:

  public int getTileCount(char tile1) {
    int count = 0;
    for (char tile: mHand.toCharArray()) {
       if (tile == tile1) {      
          count++;
        }
    }
        return count;
  }
Emeka Okoro
PLUS
Emeka Okoro
Courses Plus Student 11,724 Points

thanks . jst figured it out after running through the answers.