10

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("-------------------");
    }
}
RAS
  • 8,100
  • 16
  • 64
  • 86
user3046225
  • 109
  • 1
  • 1
  • 3
  • 3
    The runtime is telling you exactly what to do - have you tried following its instructions? (What are you expecting to get called when you run your program?) – Jon Skeet Nov 28 '13 at 14:10
  • 3
    Kindly read Error once again... – Zohaib Nov 28 '13 at 14:11
  • I tried inputting the "public static void main(String[] args)" but after running the code, it gave me so much errors, i think i have to change "void" or something. real sorry, still a student. – user3046225 Nov 28 '13 at 14:14
  • The class you name on the `java` command must contain a `main` method, with the stated attributes. This is where your program begins execution. The `main` can immediately call off to another method, or even another class, but without `main` the `java` command doesn't know where to start. – Hot Licks Nov 28 '13 at 14:15
  • 1
    You probably entered the "public static void main(String[] args)" without an associated method body. It's a method, just like your `setNumber` is a method. – Hot Licks Nov 28 '13 at 14:16
  • 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 – user3046225 Nov 28 '13 at 14:17
  • It is telling you that your `main` method needs a method body. – Stephen C Nov 28 '13 at 14:22
  • i mean, i can't figure out where to put the main method. – user3046225 Nov 28 '13 at 14:24
  • What's the main class your are trying to execute and how? For sure if the main class is Calculate, you don't have it, at least I can't see it. Add the main method and then provide us the new behavior – Enrico Giurin Aug 02 '17 at 23:31
  • I think this the problem of classpath. When I set the classpath correctly the problem went away. – Noby Nirmal Oct 22 '18 at 20:54

5 Answers5

24

Restart your IDE and everything will be fine

Rohit kumar
  • 249
  • 2
  • 2
  • 3
    Sometimes most complex of the solution are in easiest of the steps. :) – User Mar 19 '16 at 11:13
  • 1
    in my case, the eclipse's "Run as" configuration suddenly switched to a newly created class and half an hour I couldn't understand why it requires main method in non main class. Although restarting didn't help, you gave me a clue to check IDE settings. – Liphtier Jun 14 '16 at 13:15
  • That solved the issue for me as well :) – Ouissal Jun 03 '17 at 16:59
13

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.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
5

My suggestions :

  • Keep the program modular. Keep the Calculate class in a separate Calculate.java file and create a new class that calls the main method. This would make the code readable.
  • For setting the values in the number, use constructors. Do not use like the methods you have used above like :

    public void setNumber(double fnum, double snum){ this.fn = fnum; this.sn = snum; }

    Constructors exists to initialize the objects.This is their job and they are pretty good at it.

  • Getters for members of Calculate class seem in place. But setters are not. Getters and setters serves as one important block in the bridge of efficient programming with java. Put setters for fnum and snum as well

  • In the main class, create a Calculate object using the new operator and the constructor in place.

  • Call the getAnswer() method with the created Calculate object.

Rest of the code looks fine to me. Be modular. You could read your program in a much better way.

Here is my modular piece of code. Two files : Main.java & Calculate.java

Calculate.java

public class Calculate {


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

    public double getFn() {
        return fn;
    }

    public void setFn(double fn) {
        this.fn = fn;
    }

    public double getSn() {
        return sn;
    }

    public void setSn(double sn) {
        this.sn = sn;
    }

    public char getOp() {
        return op;
    }

    public void setOp(char op) {
        this.op = op;
    }



    public Calculate(double fn, double sn, char op) {
        this.fn = fn;
        this.sn = sn;
        this.op = op;
    }


public void getAnswer(){
    double ans;
    switch (getOp()){
        case '+': 
            ans = add(getFn(), getSn());
            ansOutput(ans);
            break;
        case '-': 
            ans = sub (getFn(), getSn());
            ansOutput(ans);
            break;
        case '*': 
            ans = mul (getFn(), getSn());
            ansOutput(ans);
            break;
        case '/': 
            ans = div (getFn(), getSn());
            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("-------------------");
    }
}

Main.java

public class Main {
    public static void main(String args[])
    {
        Calculate obj = new Calculate(1,2,'+');
        obj.getAnswer();
    }
}
Sameer Sawla
  • 729
  • 6
  • 20
  • Confusing: "create a new class that calls the main method." what main method again? – Ingo Nov 28 '13 at 14:47
  • @Ingo: Is that clear now? – Sameer Sawla Nov 28 '13 at 14:53
  • Better. Though I see no reason not to have a main in Calculate itself. (I use to have one in almost every class I do, and it just performs a very basic selftest, or something.) – Ingo Nov 28 '13 at 14:59
  • @Ingo : I agree. This actually depends on the programmer themselves. To me it just feels a bit more organized that is why I keep it as a separate file. There is no harm in keeping a main in every java file :) – Sameer Sawla Nov 28 '13 at 15:21
0

Where you have written the code

public class Main {
    public static void main(String args[])
    {
        Calculate obj = new Calculate(1,2,'+');
        obj.getAnswer();
    }
}

Here you have to run the class "Main" instead of the class you created at the start of the program. To do so pls go to Run Configuration and search for this class name"Main" which is having the main method inside this(public static void main(String args[])). And you will get your output.

Sanjukta
  • 1
  • 1
-2

you seem to have not created an main method, which should probably look something like this (i am not sure)

  class RunThis
{
    public static void main(String[] args)
    {

    Calculate answer = new Calculate();
    answer.getNumber1();
    answer.getNumber2();
    answer.setNumber(answer.getNumber1() , answer.getNumber2());
    answer.getOper();
    answer.setOper(answer.getOper());
    answer.getAnswer();
    }
}

the point is you should have created a main method under some class and after compiling you should run the .class file containing main method. In this case the main method is under RunThis i.e RunThis.class.

I am new to java this may or may not be the right answer, correct me if i am wrong