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

Alvaro Lopez
Alvaro Lopez
2,901 Points

I am not able to solve this challenge, it says it suppose to return 1 but only returns 0

I cannot solve this challenge, it says it suppose to return 1 but only returns 0

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public int getTileCount(char t){
    int count = 0;
    for (char letter : mHand.toCharArray()){
      if (mHand.equals(letter)) {
      count++;}
    }
    return count;
  }

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

2 Answers

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

Hello, you're very close. It looks like your if statement to see if they are equal is what is throwing it off. What it looks like it is trying to do is check to see if mHand is equal to the char chosen,

try this

  public int getTileCount(char t) {
    int count = 0;
    for (char letter: mHand.toCharArray()) {
      if (letter==t) {
        count++;
      }
    }
  return count;
  }
Craig Dennis
Craig Dennis
Treehouse Teacher

Rob there is a space between count and ++, I think that probably won't work....

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

You were right about that one, good catch. I was able to get it to work in the code challange, but it must have transposed in copying it over. Cntl V and Cntrl C are dangerously close to space.

Thanks!

Alvaro Lopez
Alvaro Lopez
2,901 Points

It make sense, thank you!