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

Evangelos Dimitris
Evangelos Dimitris
1,260 Points

How do i pass this test?

I dont get at all what i have to do over here so that i pass the challenge. please , some help would be good.

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 String getTileCount(){
  String count="";
    for (char tile : mHand.toCharArray()){
      char display = '-';
      if(mHand.indexOf(0) >= 0)
      {
      count += display;
      }
    count +=tile;

    }

  return count;
  }
}
Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Evangelos,

additional to my link, let me go through your code and modificate some minor discrepancies:

Let me correct the first line of code:

public String getTileCount(){

The method getTileCount() takes an argument and returns an integer not a String!!!!

The argument should be a primitive type char (let us name it "letter") that we can compare with all chars inside mHand (see later). AND donΒ΄t forget that we return an integer, that gives us the number of characters inside mHand that are equal to "letter".

So your method declaration should look like this:

public int getTileCount(char letter){

The next line is more than beatifull !!!! You create here a for loop, that goes through an array of chars of mHand. So you can compare your argument "letter" with every tile of mHand. I am sure that James Gosling is very proud of you :)

for (char tile : mHand.toCharArray()){

And we come to this line:

     if(mHand.indexOf(0) >= 0)

Here you try to use the indexOf() method that take an integer 0 ! BUT you donΒ΄t need to do this in the challenge:) Just for understanding the indexOf() method ... The indexOf() method can take a char like indexOf('a') and returns an integer where 'a' is inside a String ...

If you have a String of your name "Evangelos". The indexOf('a') will return an integer where a is in your name

String string = "Evangelos";
string.indexOf('a');

The result is

2

So 'a' is on the place 2, because String is zero based :)

I see, that I chatter too much :)

Inside your perfect coded for-loop, that creates a char array of mHand. Make an IF statement

     if(letter == tile) {
// if my argument "letter" is equal to tile of mHand
// add 1 to count
      count++; 
     }

I hope this huge amount of text isnΒ΄t too confusing ( I had 2 heineken today )

Please let me know if you need more help :)

Grigorij

2 Answers

Evangelos Dimitris
Evangelos Dimitris
1,260 Points

Thanks a lot my friend. Your example really helped me look through my mistakes and understand better some concepts. Appreciated!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Veeeery nice !!! Glad to help you :)

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Evangelos,

check this out:

[ https://teamtreehouse.com/community/need-some-clarification ]

Please let me know if you have more questions !!!

Grigorij