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

Ronit Mankad
Ronit Mankad
12,166 Points

Everything is right but still getting error!

I'm Getting this error :" The hand was "sreclhak" and 'e' was checked. Expected 1 but got 8."

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 int getTileCount(char answer){
      int count=0;

     for(char letter :mHand.toCharArray()){
       if(mHand.indexOf(answer)>=0){
        count++;
       }
     }
       return count;
     }


  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
}

3 Answers

Try it like this:

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(int i = 0; i < mHand.length(); i++){
     if(mHand.charAt(i) == tile)
       count++;
    }
    return count;
  }
}
anil rahman
anil rahman
7,786 Points

It says use the for each loop not for loop :)

Ronit Mankad
Ronit Mankad
12,166 Points

Thank you very much it works!

anil rahman
anil rahman
7,786 Points

I also added the foreach loop way, which was how it should have been written for that specific question. :)

anil rahman
anil rahman
7,786 Points
public int getTileCount(char tile){
      int count=0;

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

          if (letter == tile) {
            count++;
          }

       }
     return count;

     }

Way to go :) I almost typed it out that way but it seems easier to interpret doing it the other way.

Ronit Mankad
Ronit Mankad
12,166 Points

Thanks I tried this format and it works too!

That's good feed back. I haven't done the Java track yet, im actually doing web design. I learned Java in college so that's why my code isn't 100% with what you guys are being taught sorry.

anil rahman
anil rahman
7,786 Points

I find that the for loop is more difficult than a foreach loop in terms of understanding what's going on. It's much more compact i find and easier to read when using for each loops.