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 Strings and Chars

Vinayak Gadag
Vinayak Gadag
749 Points

getting error

compilation error.

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) {
      boolean isTile = addTile.indexOf('t') >= 0;
      if(isTile){
        return true;
      }else{
        return false;
      }
    }
}

1 Answer

First, we have this error:

Bummer! There is a compiler error. Please click on preview to view your syntax errors!

So we click on preview and find this:

./ScrabblePlayer.java:19: error: cannot find symbol boolean isTile = addTile.indexOf('t') >= 0; ^

This is the symptom of a larger problem, not understanding objects and methods.

Whenever you use a .method() like .indexOf(), or anything else that has a . and a () then you need to use it on an object. An object is an instance of a class. In this case, you tried using it on addTitle, which is a method .addTitle() found in your ScrabblePlayer class. .indexOf() is a is a method found in the String class.

The only String object we have is mHand, so change addTitle for mHand:

boolean isTile = mHand.indexOf('t') >= 0;

Next, we need to fix the 't'. What goes inside .indexOf is a char, like tile. Because tile is a variable, and not a literal String we do not need quotes around it. We use tile because that is what is passed into the .hasTile(char tile) method. Your final code should look like:

    public boolean hasTile(char tile) {
      boolean isTile = mHand.indexOf(tile) >= 0; //<--------------changed mHand and tile.
      if(isTile){
        return true;
      }else{
        return false;
      }
    }