0
public class Basic {

    public static void main (String []args){
        int first = 1;
            if (first == 1);{ 
                System.out.println("I did it");
            } 
            else {
                System.out.println("I didnt do it");
            }

I dont know what to do, is there a mistake and i followed all the steps in the tutorials i'm watching. It just says delete the token

3
  • I know this isnt a pretty clean code, im a beginner Commented May 6, 2015 at 17:25
  • 3
    (first == 1); this is incorrect, you dont need semicolons Commented May 6, 2015 at 17:26
  • Every open curly bracket must be closed. It's like math. Does (1 + 2 (3+4 make any sense? Commented May 6, 2015 at 17:28

3 Answers 3

1

Remove the semi-colon after (first == 1)

Sign up to request clarification or add additional context in comments.

2 Comments

What about the missing } ?
He just didn't put all of his code. This is what I assumed.
1

Remove the semicolon after if (first == 1);

The semicolon after makes the if statement finished and the block after {} is not a part of if.So the else part is complaing about the non-existence of if because else cannot exist without if

2 Comments

What about the missing }
OP only mentioned Syntax error, delete token “else” so I was giving answer only for that.
0

You should delete the obviously misplaced ; and finish all your opened brackets. Try this:

public class Basic 
{

    public static void main (String []args)
    {
        int first = 1;
        if (first == 1)
        { 
            System.out.println("I did it");
        } 
        else 
        {
            System.out.println("I didnt do it");
        }
    }
}

If you have problems with brackets, you can configure Eclipse to automatically put closed bracket under each other like in the example.

The semicolon you have placed there ended your if statement, so it had no effect on the code between the brackets. You can imagine (Java purists will pardon the simple explanation), that after if, there is only one command or command block allowed. The brackets will group more commands to one block.

Try this example, it will explain you how it works.

int i=1;

if (i==1)
    System.out.println("I should be here when i==1");
else
    System.out.println("Will this output be printed out? No, this is else section!");

if (i==2)
{
    System.out.println("I should be here when i==2");
    System.out.println("Will this output be printed out? No! Condition was not met, because i==1 and we are in the block");
}

if (i==2)
    System.out.println("I should be here when i==2");
System.out.println("Will this output be printed out? Yes, because the commands are not in the block!");


if (i==2);  //WATCH OUT, there is semicolon that terminated if statement
System.out.println("Will this output be printed out? Yes, because that semicolon has terminated the if statement!");

2 Comments

what signals that a block has ended?
Block starts with { and ends with }. If you don't end the block, then your compiler or IDE (Eclipse) will tell you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.