mm1985
mm1985

Reputation: 231

Java - syntax error during tutorial

I have the following code:

 /* Demonstrate the if.
Call this file IfDemo.java. */

package ifdemo;

public static void main(String args[}) {
    int a,b,c;{
    a=2;
    b=3;

    if (a<b) System.out.println("A is less than B");

    //this won't display anything if (a==b) System.out.println("You won't see this")
    System.out.println();
    c=a-b; // C contains -1
    System.out.println("C contains -1");
    if (C >= 0) system.out.println("C is non-negative");
    if (C < 0) system.out.println("C is negative");

    System.out.println();
    C=b-a; //C contains 1 
    System.out.println("C contains 1");
    if (C >=0)System.out.println("C is non-negative");
    if (C<0)System.out.println("C is negative");

}}

At the line: public static void main(String args[} ) { I get three errors: 1.Syntax error on token "void" , @ expected 2.Syntax error on tokens, classheader expected instead 3.Syntax error on tokens, misplaced constructs

I hope you guys can help me out.

Thanks in advance.

Upvotes: 0

Views: 988

Answers (7)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726609

In Java there are no free-standing functions, you are missing a class declaration outside of your main function. Here is how the structure of your code should look:

package ifdemo;

public class IfDemo { // <<== You are missing this line

    public static void main(String args[]) { // <<== You have a typo here
        .... //                         ^
        .... //           This should be a square bracket
    }

}

Also watch out for "stray" curly braces throughout your code: it is very important to have your braces balanced, otherwise the program will not compile with very strange errors.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691755

It should be String args[] and not String args[}. Or even better: String[] args, which makes it clearer that args is a variable of type String array.

And the main method should be inside a class named IfDemo. You can't just declare methods outside of a class.

Also, Java is case sensitive C and c are not the same thing. System and system are not the same thing.

Upvotes: 0

Vikas V
Vikas V

Reputation: 3196

public static void main(String args[]) {   //Its [] and not [}

        int a,b,c;

        a=2;
        b=3;

        if (a<b) System.out.println("A is less than B");


        System.out.println();
        c=a-b; // C contains -1
        System.out.println("C contains -1");
        if (c >= 0)    // Its not capital C . Its small c
            System.out.println("C is non-negative");   // Its capital S and not small s
        if (c < 0)
              System.out.println("C is negative");

        System.out.println();
        c=b-a; //C contains 1   // Its Capital 
        System.out.println("C contains 1");
        if (c >=0)System.out.println("C is non-negative");
        if (c<0)System.out.println("C is negative");

    }

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

Your method should be inside a class. Your file name suggests it should be

public class IFDemo {

I suggest you use an IDE to help you write the code. This will ensure you don't get so far with basic pieces of code missing/incorrect.

Upvotes: 0

DRastislav
DRastislav

Reputation: 1882

Class is missing,Behind int a,b,c; you have brace... you have to remove it and String should be (String[]args)

Upvotes: 0

Petar Minchev
Petar Minchev

Reputation: 47373

You have written } instead of ]

public static void main(String args[]) {

Upvotes: 0

gec
gec

Reputation: 23

Try fixing the bracket:

... public static void main(String args[]) ...

Upvotes: 0

Related Questions