1

I'm learning Java the classic way — by playing around with Karel.

But I seem to have encountered a simple problem I can't solve even with the help of Google.

I'm getting an error in Eclipse saying there's a syntax error on the token "else", and that I should delete it.

How come? The syntax in the code block above the else statement is identical.

Here's my code:

public void run() {

    putBeeper();
    if(beepersPresent()){

        move();

    } while(frontIsClear()){
        move();
        putBeeper();
    } else if(facingEast()){

        turnLeft();
        move();

    }
        }
Sweely
  • 386
  • 3
  • 15
  • Wow people, do we really need 8 responses saying the same thing? – jn1kk Nov 18 '11 at 18:52
  • 1
    @Skynorth To be fair to the answerers, 7 of 8 of them answered within the same minute, with the 8th not far behind. – Eric Wilson Nov 18 '11 at 19:01
  • I usually delete or cancel my answer when there's a *better* duplicate been posted at the same time: http://meta.stackexchange.com/questions/15775/do-you-delete-your-own-answer-when-its-a-duplicate Most others also do that. It keeps the noise down. – BalusC Nov 18 '11 at 19:10

7 Answers7

4

The else statement has to follow immediatly after the if, you have a while loop between them.

Since this was downvoted, more formal the relevant section of Java Language Specification

14.9 The if Statement The if statement allows conditional execution of a statement or a conditional choice of two statements, executing one or the other but not both.

IfThenStatement:
        if ( Expression ) Statement

IfThenElseStatement:
        if ( Expression ) StatementNoShortIf else Statement

IfThenElseStatementNoShortIf:
        if ( Expression ) StatementNoShortIf else StatementNoShortIf

The Expression must have type boolean or Boolean, or a compile-time error occurs.

stacker
  • 68,052
  • 28
  • 140
  • 210
3

Because you're trying to say while() { } else {

Why else?

(Yes, I did just do that.)

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
0

Your else matches while, not if. Not entirely clear what you are trying to do there.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
0

Probably:

while(frontIsClear())

should be

else if(frontIsClear())
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
0

What exactly are you trying to do here?

while(frontIsClear()){
        move();
        putBeeper();
    } else if(facingEast()){

        turnLeft();
        move();

While this.. else if that? Not possible.

FailedDev
  • 26,680
  • 9
  • 53
  • 73
0

You put your else after a while, this is indeed a syntax error

Try that:

public void run() {
    putBeeper();
    if(beepersPresent()){
        move();
    } 
    while(frontIsClear()){
        if (facingEast()) {
            turnLeft();
        }
        move();
        putBeeper();
    }
}
Guillaume
  • 22,694
  • 14
  • 56
  • 70
0

Really quite simple.

Your if lasts between the { and }. The else has to be exactly afterwards.

Remove the } near the while and put it somewhere else and you should be ok.

Haedrian
  • 4,240
  • 2
  • 32
  • 53