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

Nicolau Esteves
Nicolau Esteves
2,809 Points

am stuck on the second question of this activity. for some reason I don't understand what is being asked. Can I get help

it asks to to fix the method. but, i don't understand what they are asking for?

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 hasTile = tiles.indexOf("tile")!=-1;
    if(tile.isInThe("tiles")){
      return true;}
    else
      return false;
  }

}

4 Answers

Anders Björkland
Anders Björkland
7,481 Points

Hi Nicolau!

So you have passed task 1. Excellent! There you were asked to add the char variable tile to your String variable tiles in the addTile method. What you are now asked to do is check if there is any tile in your tiles variable that match the variable tile in your hasTile method.

Your String variable tiles has a method on it called indexOf. You can call it by typing tiles.indexOf(tile). You pass a character to the method, here called tile, and it will return the index of where that character is in the String. If the character is nowhere to be found in the String, the method will return -1. So as long as you don't get a -1, the String has that character.

Does this make sense? Good luck!

Nicolau Esteves
Nicolau Esteves
2,809 Points

Hey Anders. Thanks for your replay.. But, I keep trying the problem, but I seem not to get it right for some reason. Can u tell me what is wrong with what I did on the second question?

public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in

       if(tiles.indexOf(tile)!=-1){

                 return true;}
      else{
                 return false;}

}

Anders Björkland
Anders Björkland
7,481 Points

OK. So you nailed the logic here. But the exercise is not satisfied with the solution. It gives you this message:

Bummer! While you could definitely solve this using an if statement, try returning the result of the expression.

It wants you to solve it without an if-statement. Within the if-statement you have a boolean. Since your if-statement will return true if your statement is true, and false if your statement is false, the exercise wants you to cut out the middleman and directly return the statement (or expression). It will look like this:

return tiles.indexOf(tile) != -1;
shu Chan
shu Chan
2,951 Points

Thanks Anders that was the best answer! Can you or someone else break down what you mean by "Since your if-statement will return true if your statement is true, and false if your statement is false, the exercise wants you to cut out the middleman and directly return the statement"

What does it mean by "directly return the statement"? I'm still a little fuzzy on what return actually is

Anders Björkland
Anders Björkland
7,481 Points

While doing if-statements is preyty straight forward, sometimes it can be expressed in a simpler manner. Instead of returning the value true when a condition is met in an if statement, you could instead assign a boolean variable with that condition and return that variable. What I suggested was that there was no need for an if statement, or declaring a variable and instatiate it with your condition, but rather that you might simply return the condition-statement.

What I just described is three ways to the same thing, and the challenge liked one of them a bit more. So here the three examples are:

// if-statement:
if (tiles.indexOf(tile) != -1){
    return true;
} else {
    return false;
}

// declaring a variable and assigning resulting value of condition (true or false)
boolean hasTile = tiles.indexOf(tile) != -1;  
return hasTile;

// directly returning the result of the condition (if there is a tile, it will not be -1, so it will return true)
return tiles.indexOf(tile) != -1;

I was perhaps a bit fuzzy what I meant about cutting out the middle man and returning a statement. Now I have included these three different ways to the same thing of checking if a tile is present in the tiles String. Since they are all doing the exact same thing, but on a different amount of rows, there is one way that can be argued doing the task a bit better than the others, since it is short and concise and still shows what it is doing. So, when I said "directly return the statement", what I meant was return the resulting value of the condition. Then you won't need to do an if-statement with two different returns, and you won't have to assign a variable that you would then return.

Did this answer your question?