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 Storing Guesses

Nathan Kinsey
Nathan Kinsey
12,283 Points

Why .indexOf() instead of .contains()?

String.contains() returns a boolean value by default, so it seems like it would be more intuitive to use that method over this new String.indexOf() method. Why are we opting for the latter, when we define the isHit variable?

1 Answer

You could make contains() work here, but it takes a CharSequence rather than a char. So you would need to rewrite the method as:

public boolean applyGuess(CharSequence letter) {
    boolean isHit = mAnswer.contains(letter);
    . . .

I guess Craig didn't want to be bothered explaining CharSequence, so he used indexOf().

Isam Al-Abbasi
Isam Al-Abbasi
2,058 Points

jcorum I wonder how many years it takes for me to have your knowledge!! You are awesome my friend and I am really glad we have you on this awesome form :)

Thank you for your constant help and support