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

I'm stuck, The hand was 'srelchak', checked for e. Expected 1, but got a 0.

I'm a little confused on what to do. I am putting in the word through my method and looping through each character and checking to see if the index for each character is >= 0, yet I am getting this issue. Some help would be appreciated, thanks!

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: mHand.toCharArray()) {
      if (mHand.indexOf(letter) >= 0) {
       count += count;
      }
    }
    return count;
  }
}

2 Answers

Hi Rohan Sareen,

your first mistake is this condition

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

You want to check if the current letter in the for loop equals the character of tile. But what you are doing is to compare each character of a word with the word itself. For example the word is rohan and you are asking: is the r in rohan? is the o in rohan? is the h in rohan? and so forth.

Ok, now let's say you have fixed that. Your count variable will still hold the value of 0 even if it found something. Why? You initialized 0 to the variable count. Now what happens if something was found?

count += count; // which is the same as 0 = 0 + 0

And what would happen if count would have been any number but 0? You always would increase the value of count with the current value it holds which is the same as saying 'count * 2'.

You only want to increase its value by 1 if the character was found. You could use the literal 1 or use the increment operator '++' (which I prefer) to increase the value of count.

// Correct alternatives

count = count + 1;

count++; // Is saying, increase the current value of count by one

I hope this already gives you a good hint. Let me know if you got it, otherwise I will gladly help you some more!

Thanks Craig :D. Nice to hear that from you!

Thank you very much! I think I just had to take a break and do it, after doing exercises for a few hours straight I think I had a huge brain fart. It was much easier than I thought. Thanks for the help!

I know that feeling too. Glad I could help!