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 Strings and Chars

Alexander Nowak
Alexander Nowak
498 Points

Chars objective 2

Could someone please tell me where i am going wrong

This is my code, thank you

public boolean hasTile(char tile) { boolean hasTile = mHand.indexOf(tile) >=0;

   return false;

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hey Alexander,

It's actually alot more simpler than what you thought. Rather than creating a new boolean, we can just return the true or false value right away. It looks like this:

CodeSnippet.java
return mHand.indexOf(tile) >=0;

Here we check if we have the tile or not and then it will return a boolean value directly. You could store it as a boolean variable and then return that variable like you did. The only thing you did wrong was that you were returning false. If we have the letter in our mHands, we should be returning true shouldnt we? Also notice how you created a boolean variable but you never used it! instead of returning false, you shouldve returned the boolean variable you created. look at this:

CompleteCode.java
public boolean hasTile(char tile) { 
      boolean hasTile = mHand.indexOf(tile) >=0;

   return hasTile;
    }

Like you did, we check if we have the tile or not and store that boolean in the hasTile variable. Then we can return that variable. This way works however returning it directly like the first example i did above is much more efficient

Hope that helped you!

Happy coding and best of luck,

Kevin

Alexander Nowak
Alexander Nowak
498 Points

Thanks again Kevin!!

Im a bit confused about the 'return' function, I understand that it reruns the boolean value, but where does it return it to?

Kevin Faust
Kevin Faust
15,353 Points

Hey Alexander,

Good question! It returns the value to the method itself. that sounds confusing so look at this:

boolean checker = hands.hasTile('b')

Pretend that "hands" is an object we created of the current class. we call the hasTile() method and pass it a char. itll now check whether we have it or not and return a true or false value. but what do we do with this value? Well for one, we could store this value in a variable and then we can use this true or false value later on in your code. you will understand better how this return thing works as time goes on.

Kevin

Christopher Augg
Christopher Augg
21,223 Points

Nice answer! +1

I just wanted to add that you can also place the comparison in the return statement and have no need for a boolean variable.

public boolean hasTile(char tile) {
       return  mHand.indexOf(tile) >=0;
    }
Kevin Faust
Kevin Faust
15,353 Points

Yep! That was the first code snippet in my answer