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

Could not found what is needed to be written in my if else block. I can check if tile is in and get a boolean

public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in boolean isIn = tiles.indexOf(tile) != -1; if(isIn){

}else{

}
return isIn;

}

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
    boolean isIn = tiles.indexOf(tile) != -1;
    if(isIn){

    }else{

    }
    return isIn;
  }

}

2 Answers

andren
andren
28,558 Points

Nothing needs to go in your if statement, in fact no if statement is needed period. And because of that this challenge is setup to refuse any code that contains an if statement.

The expression you have written tiles.indexOf(tile) != -1 already produces the desired boolean, so you don't need to pass it to an if statement, you can just return the boolean it produces directly. Like this:

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

Or even more succinctly like this:

public boolean hasTile(char tile) {
  // TODO: Determine if user has the tile passed in
  return tiles.indexOf(tile) != -1;
}

When you return an expression like that what is actually returned is the boolean the expression results in.

Thank you for the answer. I have actually worked it out after asking the question. Guess there have been a little mix up with the video I watched.