Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Creating the MVP Counting Scrabble Tiles

isaac schwartzman
isaac schwartzman
963 Points

Stuck on task 2 of 2 Scrabble counting tiles

I'm so lost and I can't continue

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 a (char letter) {
int getCountOfTile = 0;
  int countOfTiles = 0;
  for (char x : tiles.toCharArray()){
  if (tiles.charAt(x) != -1){
  countOfTiles ++; } 
    countOfTiles == getCountOfTiles;
      }
  } }

2 Answers

Hi Isaac,

Task 1 hasn't been completed here. For that you need to Create a new method named getCountOfLetter that returns an int, and requires a parameter of type char named letter.

So, you need a public method that returns an int called getCountOfLetter which takes a char called letter. Put that code all together and you get:

  public int getCountOfLetter(char letter){
    return 0;
  }

Next, 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. You've made a good start on this as you've identified that you need a for loop and to convert tiles into toCharArray(). That loops through each letter, storing each in x at each iteration. You want to compare that x to the letter passed in. if they are equal, increment a counter variable. Again, you've done that. Return the counter variable after the loop ends.

Your test isn't quite right because you've tried to compare to -1. That's an indicator that the character is in the string/array, rather than being at that position. You tried tiles.charAt(x). Unfortunately, x is a char, not an index number but it was a good effort.

Just use double equals to compare x to letter.

To simplify your code, you'll end up with:

  public int getCountOfLetter(char letter){
    int count = 0;
    for(char l : tiles.toCharArray()){
      if(l == letter){
        count++;
      }
    }
    return count;
  }

I hope that makes sense.

Steve.

I called the char the letter l (it isn't the figure 1) as the word letter has already been used. I could have called it anything but, since it represents a single character, I used a one-letter variable name. Had letter not already been taken, I'd have used that.

Looking back, I should have used tile rather than l. That makes more grammatical sense.

for(char tile : tiles.toCharArray()){
  // do stuff
}

It can be called anything - but using something relevant is easier to read. So a single character for a char or the singular version of the array name is sensible.

But it isn't, and nor can it be, a number - you can't start a variable name with a number.

Make sense?

Steve.

[EDIT: Commenter deleted their contribution - I'll leave this here for the record anyway - srh]

shu Chan
shu Chan
2,951 Points

Thanks Steve sorry I deleted the question thinking it was a stupid question but you made a lot of sense

Hey - there's no such thing as a stupid question. These pages are designed to answer any query at all with no judgement - ask away! I'll happily answer anything as it wasn't that long ago I didn't understand the concepts covered in these courses. We can all gain from questions being asked and answers being provided so keep being curious and ask what's on your mind.

Steve. :+1: