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 (Retired) Creating the MVP For Each Loop

dereference question

i am getting these two errors when i check my code crabblePlayer.java:22: error: char cannot be dereferenced for(char letter: tile.toCharArray()){ ^ ./ScrabblePlayer.java:23: error: incomparable types: char and String if(letter == mHand){ ^ 2 errors

i understand the second one but i am confused about what the char cannot be dereferenced means could someone please clarify it?

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public void addTile(char tile) {
    // Adds the tile to the hand of the player
    mHand += tile;
  }

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
  public int getTileCount(char tile){
    int count = 0;
    for(char letter: tile.toCharArray()){
      if(letter == mHand){
          count++;
      }
    }
  }
}

2 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Dylan,

Let's see if I can clear a few things up for you. I'll paste your method below:

 public int getTileCount(char tile){
    int count = 0;
    for(char letter: tile.toCharArray()){
      if(letter == mHand){
          count++;
      }
    }
  }

So, a ScrabblePlayer has a "hand." You have called it "mHand". This is a string. We can turn a string to a character array with the "toCharArray() method.

 public int getTileCount(char tile){
    int count = 0;
    for(char letter: mHand.toCharArray()){
      if(letter == mHand){
          count++;
      }
    }
  }

For each letter in that char array, we want to compare it to "tile". If they are a match, then we can increment count.

 public int getTileCount(char tile){
    int count = 0;
    for(char letter: mHand.toCharArray()){
      if(letter == tile){
          count++;
      }
    }
  }

After we have compared each letter in "mHand" to the tile, we want to return the count;

 public int getTileCount(char tile){
    int count = 0;
    for(char letter: mHand.toCharArray()){
      if(letter == tile){
          count++;
      }
    }
    return count;
  }

I hope this helps. Let me know if you need any more clarification.

It's been a while since I programmed in Java, but to me it seems odd to use a char as a parameter in a method, and then try to loop through it inside the method. Are you sure it shouldn't be a string instead?