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 trialhamdan hassan
Courses Plus Student 1,236 Pointscan someone explain this
public boolean applyGuess(char letter){
if(hits.indexOf(letter) != -1 || misses.indexOf(letter) != -1){
throw new IllegalArgumentException(letter + " has already been guessed");
}
boolean isHit = answer.indexOf(letter) != -1;
if(isHit)
hits += letter;
else
misses += letter;
return isHit;
}
1 Answer
Michael Clark
16,324 PointsHi Hamdan
indexOf returns -1 if the specified character is not found in a string
if(hits.indexOf(letter) != -1 || misses.indexOf(letter) != -1){
throw new IllegalArgumentException(letter + " has already been guessed");
}
So with not equal (!=) this will return true if the character is in hits or misses and throw an exception that the letter has already been guessed.
Next isHit becomes true if the letter is in the answer or false otherwise
boolean isHit = answer.indexOf(letter) != -1;
The if statement then adds to hits if true and misses if false and returns isHit
if(isHit)
hits += letter;
else
misses += letter;
return isHit;