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

Orit Lieber
Orit Lieber
11,181 Points

What is wrong with this boolean return type method?

I was asked to fill in the hasTile method so it return true if the mHand String has the tile char init and false if it doesnt I cant seem to figure out why it's not compiling please help. the code is attached. thanks

ScrabblePlayer.java
public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
        mHand = "";
    }

    public String getHand() {
       return mHand;
    }

    public void addTile(char tile) {
        mHand += tile;

    }

    public boolean hasTile(char tile) {
      if (mHand.indexOf(tile)>=0){
        hasTile=true;}

       return hasTile;
    }
}

1 Answer

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

hasTile is already defined as a function. Later you use hasTile as a variable to hold a boolean value. Either you need to use a variable besides hasTile (so that it's different from the function name) or you need to not use it at all and straight out return the boolean values. Take a look at how I did it:

 public boolean hasTile(char tile) {
      if (mHand.indexOf(tile) >= 0) {
        return true;
      } else {
       return false;
      }
    }
Orit Lieber
Orit Lieber
11,181 Points

Thanks Jennifer! I used the first option and it worked :-) Thanks for helping me with this blocker.... :-)

Isam Al-Abbasi
Isam Al-Abbasi
2,058 Points

Hello Jennifer Cordell I tried to use the code you wrote but still not working!!

take a look please:

    public boolean hasTile(char tile) {
     if (mHand.indexOf(tile) >= 0);
        return true;
      } else{
       return false;
      }
}```
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Isam Al-Abbasi Hi! Your code isn't like mine. You've replaced one of my curly braces with a semicolon. Here's the line (according to what you posted)

if (mHand.indexOf(tile) >= 0);

It should be

if (mHand.indexOf(tile) >= 0) {