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

Steven Strouble
Steven Strouble
4,128 Points

I'm getting a 'cannot be dereferenced' error. Forums say char is a primitive and can't have methods called on it.

Doesn't really make any sense. It seems like the same process that was given in the video, but it's not working.

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 mHand) {
    int howMany = 0;
    for (char tile: mHand.toCharArray()) {
      if (mHand.indexOf(tile) >= 0) {
        howMany++;
      }
    }
    return howMany;
  }
}

2 Answers

Hello,

In your getTileCount function you have it defined as:

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

You've told it that your parameter is a char called mHand. However, you also have a member variable called mHand that is a String. I would recommend against calling the char mHand for that reason in addition to following the convention of only prefixing member variable with an m and a parameter passed into a function wouldn't be a member variable. With that changed, your for loop should no longer create an error. The reason you were getting that error is that you are calling toCharArray on a char and not the string that you were expecting to. This should at least get you a step closer to solving the Challenge. If you have any other questions, feel free to ask.

Steven Strouble
Steven Strouble
4,128 Points

I understand. However, with that changed, I still get the error because I'm attempting to perform 'toCharArray()' on a char, which should be a String. Understanding that I should be attempting to convert a string to an array, why does the exercise require the argument to be a char rather than a string? It would seem that if I'm passing a char, then I only need to check to see if that char is in the string, negating the need for a 'toCharArray()'.

What the function should be doing is going through the hand and counting how many times the passed in character is found. So if you have changed the parameter being passed into the function's name to be something like inTile, then the rest of your function should be fine except for your if statement. At that point you'd want to see if tile and inTile are the same character.

Steven Strouble
Steven Strouble
4,128 Points

Thanks for your help. I finally got it. 1) I was using the mHand variable but should have been calling the getHand() method. 2) I was using the indexOf() method when a simple compare would have sufficed.