Nehman
Nehman

Reputation: 43

Delete this token (else)

ok so i was playing round with some code and adding new line as i'm learning java. i got this error "syntax error token "else" delete this token".

As im new to this could some one explain this error and what i should do, so i do not make the same mistake again.

class Years {

public static void main (String[] args){

    int age = 30;

    if (age <30){

        System.out.println("you are young");

    }else{

        System.out.println("you are old ");

        if (age > 1240);
    }else{

        System.out.println("dam son your still a bady");

        if (age < 25);
        {


            System.out.println("You are Really old son!!");

        }else{

            System.out.println("you better Hide your age son!!"); 
            }
        }
    }
}

Upvotes: 0

Views: 134

Answers (4)

udinnet
udinnet

Reputation: 26

First understand how if statements work in any programming language. For example this is a good resource for you to understand if statements in java. I'm not 100% sure about what are you trying achieve by this code. But I can give you one hint. When branching, use not only else but also else if().

Upvotes: 0

neuronaut
neuronaut

Reputation: 2709

It looks like you've got an if statement that's in the wrong place. Try this instead:

if (age <30){

    System.out.println("you are young");

}else if (age > 1240){

    System.out.println("you are old ");

}else{

    System.out.println("dam son your still a bady");

    if (age < 25);
    {

        System.out.println("You are Really old son!!");

    }else{

        System.out.println("you better Hide your age son!!"); 
        }
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500495

You have an unconditional else block - you can't follow that with another else. So this is fine:

if (condition) {
    ...
} else if (otherCondition) {
    ...
} else {
    ...
}

But this isn't - because it doesn't make sense:

if (condition) {
    ...
} else {
    ...
} else {
    ...
}

An else block without a condition is meant to run unconditionally if the condition above it was not satisfied - in your case, the middle block is always run, so it's meaningless to have another.

Also note that you have:

if (age > 1240);

... which I suspect you didn't really intend. Likewise:

if (age < 25);

Both of these are if statements with empty bodies.

It's really unclear what you expect to achieve in each case - but I'd strongly advise you to have something like:

// List all the age boundaries in increasing order...
if (age < 25) {
   ...
} else if (age < 30) {
   ...
} else if (age < 1240) {
   ...
} else {
   ...
}

Now exactly one of those bodies will be executed.

Upvotes: 4

David
David

Reputation: 218827

You have a condition which doesn't make sense:

if (something) {

} else {

} else {

}

How do you expect the system to know which "else" block to use? else logically means "otherwise, do this" and effectively captures all remaining conditions. You can't do that more than once.

Upvotes: 0

Related Questions