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

Seyfullah Erdogan
Seyfullah Erdogan
1,922 Points

Can you help me with this Code Challenge! Please!

Here is the link for the challenge:

http://teamtreehouse.com/library/java-objects/creating-the-mvp/strings-and-chars.

ScrabblePlayer.java
public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
        mHand = "";
    }

    public String getHand() {
       return mHand;
    }

    public void addTile(char tile) {
      mHand += tile;
        // Adds the tile to the hand of the player

    }

    public boolean hasTile(char tile) {
    boolean hasTile = mHand.IndexOf('tile') >= 0;
      return false;
    }
}
Jesse Gravenor
Jesse Gravenor
27,272 Points

First of all you don't need to set a variable for hasTile at all. You can save some code and just return the boolean true or false value through an if/else statement.

You are using the right method, indexOf(), but your case is off. Also, indexOf() returns a -1 value when a char is not found in the string. This is in the video somewhere. Also, you don't need the quotes on the parameter of tile, because you want it's contents, not the literal word 'tile'.

I hope this helps!

Evans Attafuah
Evans Attafuah
18,277 Points

try the code below

public boolean hasTile(char tile) { boolean hasTile = mHand.IndexOf(tile) != -1; return hasTile; }

1 Answer

try this:

public boolean hasTile(char tile) {
        if ((mHand.indexOf(tile) >= 0)){
        return true;
      }else{
       return false;
      }
}
Evans Attafuah
Evans Attafuah
18,277 Points

The if and else statement might not be necessary. Write less code as much as possible. A one liner like so " boolean has_tile = mHand.IndexOf(tile) != -1; return has_tile;", produces the same results.