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

Mark Miller
Mark Miller
45,831 Points

Initialized count = 0; at the top of the file. When a new player is created, count will be zero. Invoking the method

It will not let me through.

public int getTileCount(char player) {

for(char nth_tile: mHand.toCharArray()) {
if( player.toLowerCase() === nth_tile.toLowerCase()) {
    count += 1;
  }
}

return count; }

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;
  int count = 0;

  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 player) {

    for(char nth_tile: mHand.toCharArray()) {
    if( player.toLowerCase() === nth_tile.toLowerCase()) {
        count += 1;
      }
    }
  return count;
  }
}

1 Answer

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

Hey there Mark,

You certainly got the right idea, a few things

You're trying to use the String version to cast the item to lower case, Char is actually Character.toLowerCase(player) or similar depending on the variable, however to pass this challenge, if you were using this in a main app you could throw an exception if they try to input an upper case Letter, but this really isn't needed in this case.

Secondly, be sure to create the count variable locally inside the method, making it class wide global variable will cause problems because it will return data from previous method calls, and hold on to that value, and this will lead to bugs.

Something like below

public int getTileCount(char player) {
int count = 0;
// rest of code here
}

Also, thirdly, I see that you have === to char primitive types in Java, this is how we do it in javascript or similar languages, but is Java it is just ==.

Adding these slight changes to your code and you'll pass this challenge with flying colors.

Thanks let me know if this doesn't make sense.