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

Stuck on returning the result of the expression.

Stuck on returning the result of the expression. I am trying to return the boolean statement in the hasTiles method, but every time I run it, it says that although I can solve it the way I am currently doing it, it wants me to pass the result of the expression, which I do not know how to do. I modeled my current work off of the video before this.

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; 

  }

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

    if (hasTileInTiles) {

      return hasTileInTiles;
    } else {

    return false;

    }
    }

}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi, Rahul Javangula! You're doing fantastic. Your code is just fine and does what it is supposed to do, but it is a bit verbose. For instance, let's imagine I have a variable x that is an integer. I want to return true if it is greater than 10 and false if it isn't. I could write that like this:

if(x > 10) {
  return true;
} else {
  return false;
}

But because the x > 10 is an evaluation that returns true or false when it runs it becomes a bit redundant to do it that way. I could instead do the same thing with this:

return x > 10;

This returns the evaluation of the expression directly.

Fortunately, you seem to have an innate understanding of this because you've already assigned the result of the evaluation to hasTileInTiles, and as you set it up, it is holding a boolean which is exactly what needs to be returned. So instead of all those if else statements, simply return hasTileInTiles; .

Hope this helps! :sparkles:

Adding to the perfect explanation of Jennifer above, you could also go one step further and return the expression directly, skipping the creation of the "hasTileInTiles* boolean:

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