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

Long Pham
Long Pham
1,363 Points

Incompatible types: unexpected return value return counter; ^

What am I doing wrong???

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

   }
}

2 Answers

Dan Johnson
Dan Johnson
40,533 Points

Change your return type in the getTileCount signature to be int instead of void.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Your method returns an int, because you defined the counter variable as an Integer. If you define your method with void, this method wanΒ΄t return nothing :)

See here

http://stackoverflow.com/questions/12769493/difference-between-void-and-int-string-etc