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 Strings and Chars

i dont wats wrong wth my code

i dont kno wat z wrong wth mi code

ScrabblePlayer.java
public class ScrabblePlayer {
    private String mHand;

    public ScrabblePlayer() {
        mHand += "tile";
    }
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 false;
    }
}
Alejandro Crespo
Alejandro Crespo
6,628 Points

In your code, when you create the ScrabblePlayer constructor you're saying: " mHand += "tile";." The compiler will read this as "the string mHand + the word "tile"", this happens because you have the word "tile" inside the quotation marks, so tile is being declared as a string. If you're trying to show an empty hand why not declare mHand as just quotation marks inside the ScrabblePlayer constructor. What I mean is: ScrabblePlayer(){ mHand = ""; } What is the challenge asking for?

Erick Kusnadi
Erick Kusnadi
21,615 Points

Yup what Alejandro said, should be mHand = "tile"; not mHand += "tile"; In the second version, you're adding tile to an uninitialized variable

2 Answers

This part of the challenge is asking you to take the argument tile that is being sent in the method addTile and add it to the mHand member field. So in your Construtor you should initiate your member variable with an empty string as the guys above already said.

        public ScrabblePlayer() {
        mHand = "";
    }

then your method appTile will take care of setting a value for this variable

   public void addTile(char tile) {
        // Adds the tile to the hand of the player

      mHand += tile; 
      // You could also use the long version of it:  
     //mHand = mHand + tile;

    }

You might already know but just for the sake of completion, When you add " " around a word or number, you are hard coding a literal value. In this case, "mHand += "tile" you are adding a literal value to a variable, so the result would be just the word tile. On the other hand, when you dynamically add (append), in this case using the addTile method mHand += tile; you are using a "user" input coming as an argument to the method and adding it to mHand variable. It means that mHand variable can be anything the "user" type in.

thanks guys God bless you