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

Passing char and adding to mHand field.

Error is unexpected return value pointing at i on is Tile.

If I remove return isTile, error is return value or something is expected

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
      boolean isTile = mHand.indexOf(tile) >= 0;
      if(isTile){
       mHand += tile; 
      }
     return isTile;
    }

    public boolean hasTile(char tile) {
       return false;
    }
}

3 Answers

Tom Sager
Tom Sager
18,987 Points

If the method declares that it will not return a value (via void), then it should not return a value. This is not allowed:

public void addTile() {
   ...
    return isTile;
}

Either change your method declaration (to boolean) or return null.

TJ von Stein
TJ von Stein
1,188 Points

I think I just had your same question, here was my solution.

<p>
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 = mHand.indexOf(tile) >= 0;
      if (isTile) {
       return true; 
      } else {
       return false;
    }
}


</p>

Notice that you had the right idea, but were unsure what method to put it in. Side note, I think void methods might not need return lines. Also, I would like to hear someone explain void methods and return types if they have a chance. Thanks!

@Tom Sager i did that but I am being compeled to use += method to add chars to existing string. below is my code. @TJ note I should use the += method

public boolean addTile(char tile) { // Adds the tile to the hand of the player boolean isTile = mHand.indexOf(tile) >= 1; if (isTile) { mHand += tile; } return isTile;