user3047713
user3047713

Reputation: 35

Java class doesn't have main method

I need to run a method code for GCD. My java file is called "GCD.java" and the public class is called "GCD." Yet I keep getting the message "Class GCD does not have a main method" even though I have no red explanation point circles in any of my lines. I can run the code without the method code (i.e. public static void main(String[] args)), but I need to run the code with a method. Thanks.

==========================

import java.util.Scanner;

    public class GCD
    {

        public static int getDivisor(int x, int y)
        {


        System.out.println("Greatest Common Divisor Finder");
        System.out.println();

        String choice = "y";
        Scanner sc = new Scanner(System.in);
        while (choice.equalsIgnoreCase("y"))
        {

            System.out.print("Enter first number: ");
            x = sc.nextInt();
            System.out.print("Enter second number: ");
            y = sc.nextInt();

            int secondNumber = 0;
            int firstNumber = 0;
            int Greatestcommondivisionfinder = 0;

            // x = first,  y = second
            if (x > y)
                {
                    do
        {
                        x -= y;
                        }
             while (x > y);
        do
        {
                        y -= x;
                        }
    while (y > 0);
            System.out.println("Greatest Common Divisor: " + x);
        }

            else if (y > x)
        {
        do
                        {
            y -= x;
                        }
    while(y > x);
        do
                        {
            x -= y;
                        }
    while (x > 0);
            System.out.println("Greatest Common Divisor: " + y);
        }
             else
                    {
                    int subtract;
                    do
                    {
                    subtract = (int)y - (int)x;
                    }

            while(y > x);
            int gcd;
            gcd = (int)x - subtract;
                    }


            System.out.println();
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
                }
            return 0;
          }
    }

Upvotes: 2

Views: 17095

Answers (3)

Pradeep Simha
Pradeep Simha

Reputation: 18123

Yes, as your Eclipse correctly says, you don't have main method in your GCD.java file. In-order run this class independently, you need to have main method. Otherwise you can only create Object of this class and call from other class.

Upvotes: 2

If your class is to be used as a main program, it has to implement an

public static void main(String[] args))

Method from where you can call your GCD method.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499760

It's entirely valid for a class not to have a main method - or for it to have a main method which isn't declared as public static void main(String[] args).

However, in order to treat a class as the entry point for a Java application, it needs that method, with that signature (although the parameter name can vary).

So basically, you've got a class which is fine in itself, but you can't launch on its own. You could create a separate class, e.g.

public class GcdLauncher {
    public static void main(String[] args) {
        GCD.getDivisor(0, 0); // Parameters are ignored anyway...
    }
}

Then after compilation you could run:

java GcdLauncher

Or you could add a public static void main(String[] args) method to your GCD class.

I would strongly advise you to change your getDivisor method not to have parameters though - you're not actually using them anyway...

Upvotes: 7

Related Questions