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

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

public boolean hasTile(char tile) { // TODO: Determine if user has the tile passed in String spot = " "; tiles = new String(); if ( spot == tiles){ return true; } try{ System.out.println("This is false"); }catch(IllegalArgumentException e){ System.out.printf("that is the wrong letter %s", e.getMessage()); } 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
    StringBuffer word = new StringBuffer();
    tiles += word.append(tile).toString();
  }

  public boolean hasTile(char tile) {
    // TODO: Determine if user has the tile passed in
    String spot = " ";
    tiles = new String();
    if ( spot == tiles){
      return true;
    }
      try{
        System.out.println("This is false");
      }catch(IllegalArgumentException e){
        System.out.printf("that is the wrong letter %s", e.getMessage());
      }
          return false;
   }
}

1 Answer

michaelcodes
michaelcodes
5,604 Points

Hi there! so for this challenge they want you to return an expressions result (boolean value true or false). We can return that expression directly after "return" without using any if or else statements.

  public boolean hasTile(char tile) {
    return tiles.indexOf(tile) != -1;
  }

We use the tiles.indexOf(tile) to check if our individual tile is contained within the String of possible tiles (the variable called tiles). indexOf is a method that returns either an index value (0 or greater), or a -1 (which means its not contained). So the expression "tiles.indexOf(tile) != -1;" is our boolean expression that will return true or false.

Hope this helps, if you have anymore questions let me know! Take care and happy coding