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

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

getTileCount challenge

i know im so close, but can find the problem with my tile counter, wich i defined and initialized with zero. this are the error message :

/ScrabblePlayer.java:22: error: incompatible types: unexpected return value return countMatchTile ++; ^ ./ScrabblePlayer.java:23: error: incompatible types: unexpected return value } return countMatchTile; ^ 2 errors

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 void getTileCount(){
      // countMatchTile already define aboive
     int countMatchTile = 0;
    for (char tile : mHand.toCharArray()){
      if (  mHand.indexOf(tile) >= 0){
        return countMatchTile ++;
      } return countMatchTile; 
          }
    }

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




}
Paul Yorde
Paul Yorde
10,497 Points

getTileCount is void, but you are specifying a return. Instead of void, return int.

Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

hi Poul, and thx for the answer, you mean i have to change the void for an int, like this:

public int getTileCount(){

instead of my mistake..

public void getTileCount(){

??

Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

hi Aaron, i did what you suggested, about the post-operation increment, i did not want to change the for loop, because it is part of the challenge, but i m really happy with the link that you let it at the end. nevertheless, i still got a problem, instead of the incremental , and the void, this is what i got as an error:

./ScrabblePlayer.java:24: error: missing return statement } ^ 1 error

and well, still trying...:)

Hi Aaron Pope

The challenge can easily be read like needing a return of the length of the string - that's not quite the case, however.

My answer to this thread includes a post I wrote explaining the purpose of the method in question.

Steve.

Paul Yorde
Paul Yorde
10,497 Points

Yes:

    public int getTileCount()

That says you are returning an int.

    public String getTileCount()

would return a String, etc...

    public void getTileCount()

says that you aren't returning anything. So no return is used in the body.

So, whatever you declare in your header is what you will return in the body.

2 Answers

Hi all,

I wrote a post on this a while ago that may help; have a read here.

Shout if you need more explanation - happy to help, if I can!

Steve.

Thanks, Gonzalo.

I hope the explanation helped out too. :wink:

Steve. :+1:

For reference to anyone who sees this thread in the future, I deleted my response that is mentioned by others in this thread.

Lesson to be learned: With all things coding, it's easy to provide the correct solution to the wrong problem. I'd posted a response that was technically correct for a particular situation, but it was not the problem that was being addressed by this thread.

My apologies for the confusion!

There was no need to delete anything, Aaron. As you say, this community is about learning. Deleting material may remove valuable learning opportunities. Yes, your answer wasn't on-point to the question being asked, in this scenario but it may have assisted other students reading the thread.

Steve.

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

Steve , a question (may be a silly one) why on the project named Hangman, we have 3 .java, i mean the prompter, the main, and the game?, i can see, this an interaction, but i get lost trying to understand, wich one get the "logic" and when i have to go and make a new function, or method to complete the whole structure.....some times i start coding on the wrong "place.java" ...