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

Matthew Philip Uy
Matthew Philip Uy
3,615 Points

is this really correct?

is there a more simpler or correct code other than this?

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

2 Answers

For hasTile() you need something more like this:

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

indexOf(tile) will return the position of the char if it is in mHand, otherwise it will return -1. So mHand.indexOf(tile) > -1 is a boolean expression and can be returned directly, as the return type is boolean.

Matthew Philip Uy
Matthew Philip Uy
3,615 Points

can this be use? return mHand.indexOf(tile) >= 0;

if not, why not? (sorry for asking a lot lol)

You are right. >= 0 does work!! Don't know what I was thinking. Guess I shouldn't answer questions late at night. Wow!

Matthew Philip Uy
Matthew Philip Uy
3,615 Points

I'm a bit confuse... Is 0 > -1 really false? I tried it on java-repl and it says true... can you further explain why is it false?