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

Cory Walker
Cory Walker
6,037 Points

Not sure what I'm doing wrong

need assistance

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 (tile.indexOf(tiles))
    return true;
  }

}
/*Correct the existing hasTile method to return true if the
tile is in the tiles field, and false if it isn't. You can 
  solve this a few ways, however, I'd like you to practice 
  returning the result of the expression that uses the index 
  of a char in a String*/

Just switch "tiles" and "tile", it should look like this: if(tiles.indexOf(tile)) return true;

When you are using the indexOf command, you need to first write the string, and then the character, like this: String.indexOf(char)

Cory Walker
Cory Walker
6,037 Points

First off, thank you for your assistance, I tried that, and I still get an error, here is my code:

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

Sorry for missleading you, i checked the syntax of the method again, apperantly I forgot it myself so it's good to refresh it. The method indexOf returns an integer, if the String doesn't have a char you are looking for it will return -1. So you want to write: return tiles.indexOf(tile) != -1; So what it does is if the String does not have a tile you are looking for, it will return -1, and it will return false because tiles.indexOf(tile) is written not equal to -1. If it has the char (tile) it will return a number not equal to -1 and will be true. Go to the workspace and try the method "indexOf" yourself and see how it works with jshell, you'll understand much better :)

Cory Walker
Cory Walker
6,037 Points

Thanks for all your help! This was a tough one for me, I was stuck on that problem for about a week. Thanks again!!!