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

davron imamov
davron imamov
9,373 Points

I have done everything correctly but it is still not working on the Scrabble game task 2

I have followed the task done everything correctly but it is still saying "needs to return result of the expression" can please someone help me

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
    if (tiles.indexOf(tile) >= 0) {
    return true;
} else {
    return false;
}
  }

}

2 Answers

andren
andren
28,558 Points

The result of the expression that you pass into the if statement (tiles.indexOf(tile) >= 0) can be returned directly, you don't need to place it in an if statement.

Like this:

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

That results in the same thing being returned, but is a lot less verbose.

davron imamov
davron imamov
9,373 Points

Thank you very much now i understand the concept

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

Hi there! First off, your code would work. But there's a cleaner and nicer looking way to do it and I believe that's what he's trying to reinforce here. Ideally, you never want to see something that is "If this is true, return true. If it's not, return false." What you want your code to say is something closer to "Return whether this is true or false".

Here's a simple example. Let's say we're checking to see if a number is greater than 10.

// We could write it like this
if (num > 10) {
   return true;
} else {
   return false;
}

// It would look nicer to return the evaluation directly

return num > 10;

I combined all those lines into one. When it does the evaluation num > 10 it is the result of the evaluation that is returned. Either true or false will be returned.

I think you can get it with this explanation but let me know if you're still stuck! :sparkles:

davron imamov
davron imamov
9,373 Points

Thanks a lot i have learnt new ways to make my code simple thanks