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

Andrei Musat
Andrei Musat
738 Points

incompatible types: String cannot be converted to char tile += tiles;

I don´t really know what I have to do ,I don't know how to play 'Scrabble' ,can someone help me with what I should do here more explicitly then the instructions up there please ?!

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 boolean addTile(char tile) {
    // TODO: Add the tile to tiles
    boolean isTiles=tiles.indexOf(tile) != -1;
    if (isTiles){
      tile += tiles;
    }
  }

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

}

2 Answers

Boban Talevski
Boban Talevski
24,793 Points

The solution should look like this.

public void addTile(char tile) {
    // TODO: Add the tile to tiles
    tiles += tile;
    // which is the same as
    // tiles = tiles + tile;
  }

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

In the first method addTile, you need to add the char variable tile which is passed as a parameter to the method to the variable tiles, which is a class/instance variable of type String. So, you use the shortened version for concatenating strings (and chars) += to simply add the passed tile to the tiles. Does it make more sense?

In the second method hasTile, you need to return a boolean (true or false) stating if a char tile is present in the String tiles. So, you return whether tile is contained in the tiles by using the above return statement. What it does, is check if the char is present in the String by using the indexOf method on the String and passing that char as a parameter (inside parenthesis). What that method returns is the index (location) of the char within the String if it's present, or -1 if it isn't. Therefore if we get any number which is not -1 (it can be 1, 5, 17 or anything else, we don't care), then our return statement will be true and the method hasTile will return true, meaning the char was present. If the result of the indexOf method is actually -1, that means that the char was not present in the String and our statement will then evaluate to false, since -1 is not equal to -1 is actually false.

Hope this cleared things up.

Andrei Musat
Andrei Musat
738 Points

Yes it makes sense ,thank you for the long explenation !!!

Andrei Musat
Andrei Musat
738 Points

now I get an error that says missing return statement on public boolean addTile(char tile) { // TODO: Add the tile to tiles tiles += tile; <<<< here // which is the same as // tiles = tiles + tile;

Boban Talevski
Boban Talevski
24,793 Points

Ah, right...sorry. The addTile method should not return a value. Updated my answer. Just replace boolean with void as a return type for the method and it should work.

Andrei Musat
Andrei Musat
738 Points

It worked this time ,I think I should spend way more time to learn maybe that way I will learn better .