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

Help w Scrabble challenge, getTileCount() method...

Stuck at this error, don't know why mHand.hasTile(tile) isn't working as expected - a small clue would be appreciated, thx. (Also not sure why code I paste into this form is being reformetted - trying comments to see if it will ignore.)

/* ERROR:

ScrabblePlayer.java:25: error: cannot find symbol if (mHand.hasTile(tile)) { ^ symbol: method hasTile(char) location: variable mHand of type String 1 error

CODE:

public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
        mHand = "";
    }

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

  // return number of matched characters
    public int getTileCount() {
        int count = 0;
        for (char tile: mHand.toCharArray()) {
            if (mHand.hasTile(tile)) {      
                count++;
            }
        }
        return count;
    }
}

*/

Craig Dennis
Craig Dennis
Treehouse Teacher

If you use three back ticks ` followed by java it will color code correctly. Make sure to mark Kristian's answer as best answer, if it worked for you! They deserve points!

Craig Dennis
Craig Dennis
Treehouse Teacher

If you use three back ticks ` followed by java it will color code correctly. Make sure to mark Kristian's answer as best answer, if it worked for you! They deserve points!

1 Answer

Kristian Gausel
Kristian Gausel
14,661 Points

Because mHand is a string and therefore does not have the method hasTile in this code:

int count = 0; for (char tile: mHand.toCharArray()) { if (mHand.hasTile(tile))

If this is used in a method in the class ScrabblePlayer just do: if(hasTile(tile))

Many thx KG, makes sense - that let it compile without error.