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

Issue with hasTile Method in ScrabblePlayer Class?

Alright, I think I tried every possible way. It's not like I'm new to Java, I've been doing it to remember the stuff I knew. But this one drives me crazy, either I get Syntax Error which I cannot even preview OR "Bummer! While you could definitely solve this using an if statement, try returning the result of the expression."

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) {
      boolean isField =tiles.indexOf(tile) >= 0;
      if(isField) {
           return true;
      } else {
           return false;
      }
   } 

}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey there,

The second error you mentioned is the key to what is wrong. While your code would probably work, it is way more than what the challenge wants from you. All it wants is a return statement. So, inside the method there should only be one line of code (a return statement). Everything you need to pass is there and is correct, you just need to get rid of the if/else statement and ... one more hint: the challenge didn't ask you to declare a variable isField.

Give it another go with this in mind. :) :dizzy:

Hey, your input kinda refreshed my mind and started looking from another angle, so Thank you. I still left isField variable and it worked.

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