Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Main method not found in class Calculate, please define the main method as: public static void main(String[] args) [duplicate]

Tags:

java

methods

main

I need help with the main method, I'm getting this error:

Error: Main method not found in class Calculate, please define the main method as:
    public static void main(String[] args)

Here's the code:

class Calculate {

private double fn;
private double sn;
private char op;

public void setNumber(double fnum, double snum){
    this.fn = fnum;
    this.sn = snum;
}
public double getNumber1(){
    return fn;
}
public double getNumber2(){
    return sn;
}
public void setOper(char oper){
    this.op = oper;
}
public char getOper(){
    return op;
}
public void getAnswer(){
    double ans;
    switch (getOper()){
        case 'a': {
            ans = add(getNumber1(), getNumber2());
            ansOutput(ans);
            break;
        }case 'b': {
            ans = sub (getNumber1(), getNumber2());
            ansOutput(ans);
            break;
        }case 'c': {
            ans = mul (getNumber1(), getNumber2());
            ansOutput(ans);
            break;
        }case 'd': {
            ans = div (getNumber1(), getNumber2());
            ansOutput(ans);
            break;
        }default:
            System.out.println("--------------------------");
            System.out.println("Invalid choice of operator");
            System.out.println("--------------------------");
        }
    }
    public static double add(double x,double y){
        return x + y;
    }
    public static double sub(double x, double y){
        return x - y;
    }
    public static double mul(double x, double y){
        return x * y;
    }
    public static double div(double x, double y){
        return x / y;
    }

    public static void ansOutput(double x){
        System.out.println("----------- -------");
        System.out.printf("the answer is %.2f\n", x);
        System.out.println("-------------------");
    }
}
like image 251
user3046225 Avatar asked Nov 28 '13 14:11

user3046225


2 Answers

Restart your IDE and everything will be fine

like image 189
Rohit kumar Avatar answered Oct 18 '22 18:10

Rohit kumar


From the docs

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".

As you say:

error: missing method body, or declare abstract public static void main(String[] args); ^ this is what i got after i added it after the class name

You probably haven't declared main with a body (as ';" would suggest in your error).

You need to have main method with a body, which means you need to add { and }:

public static void main(String[] args) {


}

Add it inside your class definition.

Although sometimes error messages are not very clear, most of the time they contain enough information to point to the issue. Worst case, you can search internet for the error message. Also, documentation can be really helpful.

like image 35
Melquiades Avatar answered Oct 18 '22 18:10

Melquiades