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.

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