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 Creating the MVP Scrabble Tiles

Enoch Kwofie
Enoch Kwofie
1,083 Points

doing one of the task where it says to return true if tile is in tiles and false if its not need help solving it

I'm struggling to solve the answer of how to do it. its asking me to Correct the existing hasTile method to return true if the tile is in the tiles field, and false if it isn't. You can solve this a few ways, however, I'd like you to practice returning the result of the expression that uses the index of a char in a String but tried different ways but still not the right one

ScrabblePlayer.java
public class ScrabblePlayer {
  // A String representing all of the tiles that this player has
  private String tiles;

  public ScrabblePlayer() {
    tiles = "";
  }

  public String getTiles() {
    return tiles;
  }

  public void addTile(char tile) {
    // TODO: Add the tile to tiles
tiles +=tile;
  }

  public boolean hasTile(char tile) {
    // TODO: Determine if user has the tile passed in
    String itIsIn;
    boolean isIn = ItIsIn.indexOf(tile)!=-1;
    if (isIn){
      return true;
    }else {
      return false;
    }
    return isIn;
  }

}

1 Answer

Cam Richardson
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree
Cam Richardson
Front End Web Development Treehouse Moderator 16,895 Points

Hey there!

One thing I notice is that you're declaring but not defining a String called itIsIn. Since you didn't set it to something, trying to call methods (like indexOf) will cause you some headache. Instead of calling indexOf on itIsIn, we can get rid of that variable entirely and call it on the tiles variable that is already defined previously in the class, like this:

public boolean hasTile(char tile) {
    boolean isIn = tiles.indexOf(tile) != -1;
    if (isIn) {
      return true;
    } else {
      return false;
    }
  }

This should help. However, we can go a step further. By specifying to return true if isIn is true and false if it is false, we're being a little redundant. We don't need to say return true or return false because isIn is already either true or false! We can just say:

public boolean hasTile(char tile) {
    boolean isIn = tiles.indexOf(tile) != -1;
    return isIn;
}

Or even:

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

Of course those last two methods are optional. Hopefully that helps to clear up some things.

Enoch Kwofie
Enoch Kwofie
1,083 Points

thank you this has made more sense now thank you