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 Counting Scrabble Tiles

Christopher Downie
Christopher Downie
2,546 Points

I'm not understanding this task, Can someone explain this to me.

For some reason i am not understanding how to approach this task, Can someone explain this to me providing the code please. Challenge is " Now in your new method, have it return a number representing the count of tiles that match the letter that was passed in to the method. Make sure to check Example.java for some example uses. "

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) {
    tiles += tile;
  }

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

  return 0;  }


}

4 Answers

Hi there,

You want to iterate over each character in the tiles string. To do this, convert it to an array using the toCharArray() method on the tiles string. Next, create a for loop to iterate over each element of that array. Within the loop, you will pull out each element in turn and store that in a temporary, local variable; I called it tile. On each loop, see if tile is equal to char, the letter passed into this method. If they are the same, add one to a counter variable. At the top of the method, declare and initialise this int variable.

Once the loop has completed, return the count variable (your method returns an int) and that should be it! My code looks like:

  public int getCountOfLetter(char letter){
    int count = 0;                         // create & init a counter
    for(char tile: tiles.toCharArray()){   // convert to array and create a local var & loop
      if(tile == letter){                  // compare the char to tile
        count++;                           // increment if equal
      }
    }
    return count;                          // return the result
  }

I hope that makes sense!

Steve.

Christopher Downie
Christopher Downie
2,546 Points

Thank you so much, I now understand.

:+1: :wink:

Chad Lewis
Chad Lewis
1,191 Points

Thank you, I struggled wrapping my head around the solution to this and your answer made the solution very clear and understandable. Much obliged.

Glad to have helped! :+1:

gmilan
gmilan
9,287 Points

in your new method getCountOfLetter(). have it return the number of tile (the player has) that have the same letters (of the letter passed in the method). return countOfTiles; (don't forget to imagine a bunch of tiles and every tile has a letter. even if you "line up" the tiles to make a string of letters the letters are still separated into an individual tile.) so.... for a tile in an array of tiles { if (a letter is equal to any of the tile) { countOfTiles++ return countOfTiles;

Thank you. I had the exact same issue.

:+1: :smile: No problem!

I love the explanation opposed to just getting the answer. Thanks a mil!