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

Hey could anyone give me some guidance im really not sure where i going wrong?

any help much appreciated :)

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) {
    tiles += tile;
    // TODO: Add the tile to tiles

  }

  public boolean hasTile(char tile) {
    // TODO: Determine if user has the tile passed in
    boolean inTiles = tiles.indexOf(tile) != -1; 
    boolean notInTiles = tiles.indexOf(tile) != 1;
    if (inTiles) {
      return true;
      else if (notInTiles) {
        return false;
      }
    }
  }
isaac schwartzman
isaac schwartzman
963 Points

Hey James, what part do you need help with?

1 Answer

Caleb Kemp
Caleb Kemp
12,754 Points

Okay, I think it was pretty obvious that you were having trouble with step 2. I solved it very quickly using an "if" statement, however, I'm sure as you've noticed, the directions specify no "if" statements allowed. At first I thought, this is so stupid, in order to determine if something is true or false, you have to use an if statement somewhere. So, I figured, I guess I'll have to use a method with an "if" statement built in. Seeing how you used the "indexOf" method, I thought I would try that first. By pairing that with the "charAt", ".equals", and "Character.toString()" methods, I got it to work. However, I wasn't satisfied with the solution. Sure it worked great, provided it could find a character, but it would create a syntax error if it didn't find the char (index == -1). So, despite that passing the code challenge I decided to look for something else. I finally settled on this solution.

public boolean hasTile(char tile) {
    return (tiles.indexOf(tile) >= 0);
  }

Essentially the way this works, is, if index == -1, (char not found), it will return false, if it found the char (index will be 0 or greater), return true. Hope it helps