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

Fixing the hasTile method for scrabble

I am on the second part of the scrabble challenge where it is asking me to use the index of a string to run an elif function. This is in order to append a new tile to an existing set of tiles (I guess to make only an actual word legal?)

Im having to modify the hasTile method to allow for a boolean to run the elif; to my understanding I am checking whether the new tile has an index value thats positive so it deems it legal.

Then if it has a value of -1 it will return false and wont append the tile?

Any thoughts are much appreciated!

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

  public ScrabblePlayer() {   //Constructor
    tiles = "";   //initialise tiles
  }

  public String getTiles() {
    return tiles;
  }

  public void addTile(char tile) {    //input of new tile
    tiles += tile;    //appends the new tile

  }

  public boolean hasTile(char tile) { 
    boolean tiles = tiles.indexOf(tile)    //the boolean for tiles is only true for a tile found in tiles 
      if (char tile) != -1; {
        return true;
      } else {
        return false;
      }  
  }

}

1 Answer

Steven Parker
Steven Parker
229,670 Points

You have the right idea, but a few implementation issues:

  • "indexOf" doesn't return a boolean, it returns an int
  • the name "tiles" is already being used
  • the assignment statement needs a semicolon at the end
  • the parentheses should enclose the entire "if" comparison
  • the "if" should not contain a type declaration
  • there should not be a semicolon before the brace that begins the "if" code body
  • the "if" could technically work, but the challenge wants you to return the comparison result directly
return tiles.indexOf(tile) >= 0;

This line of code solved it; am I right in understanding this as saying that if the tile has a value that isn't -1 it therefore exists to be used in game?

Steven Parker
Steven Parker
229,670 Points

I assume you mean "if indexOf has a value that isn't -1", and that's exactly right, and good job!

Hello Steven,

yes I am referring to the indexOf, sorry.

I think in my head I read: indexOf(tile) as 'the index of (the tile)'; is this an apt way to do this.

Im finding this come up more often in my understanding of java: where I understand the logic and the order of processes that are intended to the code/projects (I am currently on the hangman exercise with Craig Dennis).

But Im finding the syntax challenging at times and as a result; I get lost on what I'm reading and what actions the code may be referring to or performing. This probably comes down to practice at the end of the day, but is there any way to breakdown the code visually or methods programmers use to gain more confidence in the syntax of java? I have struggled on the same problem with javascript in the past as well.

I am confident however, that the syntax it's starting to sink in and I am piecing more of the language of java as a result thank you for your continued feedback and support!

Many Thanks Louis