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

Android Build a Simple Android App (2014) Coding the Fun Facts Using Conditionals (else Statements)

Why is my else if statement showing up as an error?

if (randomNumber == 0); { fact = "ants stretch in the morning"; } else if (randomNumber == 1); { fact = "Ostriches can run faster than horses"; } else if (randomNumber == 2);{ fact = "Olympic gold medals are mostly made out of silver"; } else{ fact = "sorry there was an error"; }

else is showing up as an error and said 'else' without 'if' idk what that means.

1 Answer

David Omar
David Omar
5,676 Points

Think its because your putting semicolons after your if and else if statements. Remove the semi colon after the parenthesis

if (randomNumber == 0) {
    fact = "ants stretch in the morning"; 
} 
else if (randomNumber == 1) { 
    fact = "Ostriches can run faster than horses"; 
} 
else if (randomNumber == 2) { 
    fact = "Olympic gold medals are mostly made out of silver"; 
}
 else { 
    fact = "sorry there was an error";
 }

edit: you were right, thanks alot!

David Omar
David Omar
5,676 Points

Did you try out my solution? Sometimes an error comes from something that happens before the actual line that is outputting the error. Putting the semicolon after the parenthesis terminates the statement