0

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.

2
  • Be careful with the case of variables in Java. It's if (c >= 0) etc. Also encapsulate the code in a class Commented Jul 21, 2013 at 10:02
  • Please click edit and post your modified code after the update. Commented Jul 21, 2013 at 10:48

7 Answers 7

1

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.

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

4 Comments

OK thanks. I editted the code but I stil have one error: "The public type ifdemo must be defined in its own file"
@mm1985 Please edit the question and post your updated code, preferably in a separate block of code. Click the "edit" link under your question to enter the edit mode.
@mm1985 "The public type ifdemo must be defined in its own file" means that your file name is not "ifdemo.java". Make sure the file name and the class name match
@mm1985 You need to call your file IfDemo.java
0

Try fixing the bracket:

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

Comments

0

You have written } instead of ]

public static void main(String args[]) {

Comments

0

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

1 Comment

actually, this part is fine. Useless, but fine.
0

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.

Comments

0
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");

    }

Comments

0

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.

1 Comment

Suprised that you missed the missing class. ;) Well, there are hell lot of errors.

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.