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

Steve DeYoung
Steve DeYoung
1,313 Points

Needs clarification on this method (validateGuess)

The rest of the method makes sense but I would like some clarification on this part. How does this check to see if a letter is already guessed?

if (mMisses.indexOf(letter) >= 0 || mHits.indexOf(letter) >= 0){
  throw new IllegalArgumentException(letter + " has already been guessed");

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,364 Points

Hi Steve,

the indexOf method returns an integer that represents the index position of a spesific letter inside mMisses or mHits.

If letter haven´t been guessed yet, the letter is not inside mHits or mMisses (no index position). The indexOf method will return -1 (no index found).

Every returned integer higher or equal then 0 triggers the exception. If the returned integer is 0, then the letter is on the first index inside mHits or mMisses.

Makes sense?

Grigorij

Steve DeYoung
Steve DeYoung
1,313 Points

Ah ok.. Yes makes sense now. Thanks!