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

Got stuck. Scrabble Player.

Please guys, anyone can help me out?

Thanks!

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public int getTileCount(char letter) {
    int tileCount = 0;
    for (char tile: mHand.toCharArray()) {
      if (mHand.indexOf(tile) >= 0) {
        letter = tile;
        tileCount += 1;
      } 
    }
    return tileCount;
  }

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
}

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Thiago,

you are very close !

You need to modify your conditional if statement inside the for loop:

   if (mHand.indexOf(tile) >= 0) {
        letter = tile;
        tileCount += 1;
      } 

Here you try to compare a String with a char. But you should compare a char with a char. But donΒ΄t panic, you have everything you need in this block of code :)

You already created a beautifull charArray of mHand in your for loop. This chars can be compared with the char "letter" you have as argument. For your if statement use the == operator to compare your letter with every char of the mHandArray. So your conditional if statement should look like this:

      if (letter == tile)  {
        tileCount += 1;
      } 

I hope this will do the trick !

Grigorij

Thanks Grigorij! I'll try it!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey, let me know if you need more help.

We can go through the code step by step !

Grigorij

I appreciate that!