Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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