Your compiler error, 'else' without 'if'
is because you have something like this:
if(isOkay)
doSomething();
andThenDoSomethingElse();
else
doAnotherThing();
andEvenSomethingElse();
And you must put each block in curly braces, like this:
if(isOkay) {
doSomething();
andThenDoSomethingElse();
} else {
doAnotherThing();
andEvenSomethingElse();
}
I strongly recommend using curly braces in all if
s (and whiles
and for
s and everything else), even when there’s only a single statement.
More information can be found at this question: Else without if
2
solved Java program. How do I loop my “isLucky” method through my array? Also, how do I print my results correctly in the main method?