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

Taebin You
Taebin You
4,786 Points

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

Why wouldn't this work?

I cannot understand...

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 String getTileCount() {
    int counter = 0; 
    for (char tile: mHand.toCharArray()) {
      if(mHand.indexOf(tile) >= 0){
        counter++; 
      } 
    }
    return Integer.toString(counter);
  }
}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're actually fairly close here. But your method should be accepting a char and returning an integer. Your method accepts nothing and returns a string. I posted a fairly detailed explanation and solution here quite a while ago. Take a look and see if it helps clarify things! Good luck! :sparkles:

Taebin You
Taebin You
4,786 Points

Is there any way that I can use .indexOf instead of if ==? I am trying to use indexOf with your answer, but can't figure it out. I am not quite sure how I how I should use it

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Not that I'm aware of. Keep in mind you're looking at two tiles and see if they match. You're looking at that tile in that spot of your hand and comparing it to the tile you're search for. Take a look again at my solution linked above.