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

So back to that ScrabblePlayer. I found that it's not enough to know if they just have a tile of a specific character. W

The error says "Expected 1 but got 8"

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

}

2 Answers

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

Hey Luke,

The trouble with your code is the if statement.

Right now what it is checking to see if checking each character in the hand, and then doing another check to see if that character exists in the hand, which will always return true since we're first asking it first if the char at mHandArray[0] is contained in mHand array, which will be all correct, or all incorrect. as it will increment the count each time if it finds it.

Try to change your if statement to something like.

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

Should fix the troubles, let me know if it doesn't, or if there's anything I can better explain.

Seth Kroger
Seth Kroger
56,414 Points

You don't want to test whether the tile is in the hand. That will remain true or false for all the iterations of the loop and the result will either be 0 or 8. To count the number you need to see if each letter is equal to the tile passed in and count it if it is.