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

Sara Pasinato
Sara Pasinato
1,023 Points

I don't understand what I miss in the codes ; Please , Anyone can help me???

public class ScrabblePlayer { // A String representing all of the tiles that this player has private String tiles; private boolean answer;

public ScrabblePlayer() { tiles = ""; }

public String getTiles() { return tiles; }

public void addTile(char tile) { this.tiles +=tile;

}

public boolean hasTile(char tile) { boolean isTile= tiles.indexOf(tile)!= -1; if (isTile){ answer = true; }else{ answer=false; } return isTile;

}

}

ScrabblePlayer.java
public class ScrabblePlayer {
  // A String representing all of the tiles that this player has
  private String tiles;
  private boolean answer;

  public ScrabblePlayer() {
    tiles = "";
  }

  public String getTiles() {
    return tiles;
  }

  public void addTile(char tile) {
    this.tiles +=tile;

  }

  public boolean hasTile(char tile) {
    boolean isTile= tiles.indexOf(tile)!= -1; 
      if (isTile){
          answer = true;
         }else{
          answer=false;
          }
        return isTile;


  }

}

4 Answers

Hi Sara,

Yes, you don't need the if statement. You are creating a boolean, isTile and then returning true if it is true or false if it is false. That's duplication of code.

Just return the test:

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

Here, the indexOf() method returns the position in tiles where tile exists, or -1 if tile isn't in tiles. So, you can test for != -1 or >= 0, that amounts to the same thing! Either comparison will evaluate to a boolean which you can return straight out of the method. That's what the compiler is looking for.

Make sense?

Steve.

Sara Pasinato
Sara Pasinato
1,023 Points

oh!?thanks ! now it is clear. I must be more careful.. Sara

No problem! Your solution would work; but the challenge is looking for a refactor to eliminate lots of unnecessary code. If you find yourself returning true or false from an if statement, you often don't need the if statement at all. One little point for the future! :smile:

As long as you understand the point, that's the main thing - you've done the learning part, which is cool! :+1:

Steve.

Sara Pasinato
Sara Pasinato
1,023 Points

Thanks for your hints??, but the compiler says that I have produced this error: " While you could definitely solve this using an if statement, try returning the result of the expression." This is the request of the test: Correct the existing hasTile method to return true if the tile is in the tiles field, and false if it isn't. You can solve this a few ways, however, I'd like you to practice returning the result of the expression that uses the index of a char in a String.

Sara Pasinato
Sara Pasinato
1,023 Points

ok ? thanks Steve. Sara

Sameera Sy
Sameera Sy
2,349 Points

Your if block needs a refinement. Check the code below.

public boolean hasTile(char tile) {
    if(tiles.indexOf(tile)!=-1){
        answer = false;
    }
    else{
        answer = true;
    }
    return answer;
 }

Cheers!