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!!"); 
            }
        }
    }
}
Jon Skeet
people
quotationmark

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.

people

See more on this question at Stackoverflow