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

Very confused by the method that the instructor wishes me to use

I have been trying to solve this for over an hour. I have looked back at the video many times. I have done what he did. What am I doing wrong here?

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
    boolean isATile = tiles.indexOf(tile) != -1;

    if (isATile) {
      return true;
    }

    else {
      return false;
    }

  }

}

1 Answer

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hello Nate Maroof

Sure thing you can get lost and feel confused but when you do don't hesitate to ask.

This is how I approached the problem. I first created a boolean hasLetter and set it to false. I then went for the for-loop to iterate through the characters contained inside tiles. I used String.toCharArray().length method to convert the tiles to an array of characters and get the number of letters.

Lastly, I used String.indexOf( char) which returns -1 if the character is not found, if character is found, it returns a number that is greater or equal to zero.

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

    boolean hasLetter = false;
    for(int i =0; i<tiles.toCharArray().length; i++){

   hasLetter = ( tiles.indexOf(tile) >=0);

    }
    return hasLetter;

Hope this will help you.

Thank you so much!!