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 trialAlexis Ruiz
9,237 PointsUnreachable statement
I'm getting this error and I can't solve it :( I still don't see anything wrong in my code. Can someone help me?
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
./Game.java:23: error: unreachable statement
boolean isHit = answer.indexOf(letter) != -1;
^
1 error
public boolean applyGuess(char letter){ if (misses.indexOf(letter) != -1 || hits.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
andren
28,558 PointsYou have placed a semicolon after your if
statement declaration:
if (misses.indexOf(letter) != -1 || hits.indexOf(letter) != -1);
if
statements should not have semicolons added. The semicolon tells Java that you are done with your if
statement, which means that it treats the next block of code (which is supposed to be a part of the if
statement) as regular code that should always run. Which means that your code will always throw an exception.
Once an exception is thrown the method stops executing, which means that the code below it is indeed unreachable. If you simply remove that semicolon that error should go away.
Alexis Ruiz
9,237 PointsAlexis Ruiz
9,237 PointsThank you for your fast and accurate response. This makes me feel so ashamed LOL.