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

SAMI ULLAH NAIKOO
SAMI ULLAH NAIKOO
765 Points

want to know mistake in the following code

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;} }

}

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;}
  }

}

1 Answer

andren
andren
28,558 Points

There are two issues:

  1. In the addTile method you wrote in task 1 you are adding the string "tile" to the tiles variable instead of adding the contents of the tile variable like you are meant to. You are only meant to use double quotes when you want to create a string, when referencing a variable you should just type the name of it.
  2. The code in the hasTile method you wrote in task 2 is technically fine as it would actually work correctly, but it is inefficient to use an if statement when the expression you have written returns the right Boolean on it's own. You can return the expression directly, which is more efficient and is what the task is looking for.

If you fix those two issues like this:

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; // Remove quotes around the `tile` variable
  }

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

Then your code will be accepted.