3

Can someone please help me with the code below it builds without any error but when i Run it i get "Error: Main method not found in class Program, please define the main method as: public static void main(String[] args)"...

any ideas why ?

    import java.util.*;

public class Program
{
    // Create an array/database for 100 prizes We can increase this number


private Prize[] prizeDatabase = new Prize[100];
        // variable to store the next available position in the database
        private int currentArrayPosition = 0;

        /** 
         This method displays the menu to the user
        */
        private void DisplayMenu()
        {
            System.out.println("Please select an option from the following:");
            System.out.println("1. Enter details of a prize");
            System.out.println("2. Print the details stored for all prizes");
            System.out.println("3. Search for a prize by description or value");
            System.out.println("4. Quit");

            // Get the users selection
            String selection = new Scanner(System.in).nextLine();

            if (selection.equals("1"))
            {
                EnterPrizeDetails();
            }
            else if (selection.equals("2"))
            {
                PrintPrizes();
            }
            else if (selection.equals("3"))
            {
                SearchPrize();
            }
            else if (selection.equals("4"))
            {
                // do nothing, console will exit automatically
            }
        }

        /** 
         Search a prize
        */
        private void SearchPrize()
        {
            System.out.println("Please enter search term and press enter: ");
            // get the users search term from the console
            String searchTerm = new Scanner(System.in).nextLine();

            System.out.println("Your following matches are: ");

            // Loop round all the prizes
            for (Prize prize : prizeDatabase)
            {
                if ((prize != null))
                {
                    // if the prize matches the users search criters then print the prize
                    if (prize.MatchPrize(searchTerm))
                    {
                        prize.PrintPrize();
                    }
                }
            }

            System.out.println();
            DisplayMenu();

        }

        /** 
         Print all prizes in the database
        */
        private void PrintPrizes()
        {
            System.out.println("The following prizes are in the database: ");

            for (Prize prize : prizeDatabase)
            {
                if (prize != null)
                {
                    prize.PrintPrize();
                }
            }

            System.out.println();
            DisplayMenu();
        }

        /** 
         Enter a prize and store it into the database
        */
        private void EnterPrizeDetails()
        {
            // Take user input and store it to the enter the prize into the database

            System.out.println("Please enter a description of the prize and then enter");

            String description = new Scanner(System.in).nextLine();

            System.out.println("Please enter the colour of the prize and then enter");

            String color = new Scanner(System.in).nextLine();

            System.out.println("Please enter a value of the prize and then enter");

            String value = new Scanner(System.in).nextLine();

            Prize newPrize = new Prize();
            newPrize.SetPrizeDetails(description, color, value);

            // Check if we can add the prize to our database
            if (currentArrayPosition < 100)
            {
                prizeDatabase[currentArrayPosition] = newPrize;
                currentArrayPosition++;
                System.out.println("A prize has successfully been added to the database.");
            }
            else
            {
                System.out.println("Please contact admin to increase the size of the database");
            }

            System.out.println();
            DisplayMenu();
        }

        static void main(String[] args)
        {
            Program program = new Program();
            program.DisplayMenu();
        }
    }










     class Prize extends Program
    {

        private String privateDescription;
        private String getDescription()
        {
            return privateDescription;
        }
        private void setDescription(String value)
        {
            privateDescription = value;
        }
        private String privateColor;
        private String getColor()
        {
            return privateColor;
        }
        private void setColor(String value)
        {
            privateColor = value;
        }
        private String privateValue;
        private String getValue()
        {
            return privateValue;
        }
        private void setValue(String value)
        {
            privateValue = value;
        }

        public Prize()
        {

        }

        /** 
         Setter method to set all the prize details

         @param description
         @param color
         @param value
        */
        public final void SetPrizeDetails(String description, String color, String value)
        {
            setDescription(description);
            setColor(color);
            setValue(value);
        }

        /** 
         Check if a search term matches the prize

         @param searchTerm
         @return 
        */
        public final boolean MatchPrize(String searchTerm)
        {
            boolean match = false;

            // Check if the search matches colour or value and return true if it does
            if (searchTerm.equals(getColor()) || searchTerm.equals(getValue()))
            {
                match = true;
            }

            return match;
        }

        /** 
         Print out the  details of the prize
        */
        public final void PrintPrize()
        {
            System.out.println("Description: " + getDescription() + " Colour: " + getColor() + " Value: " + getValue());
        }
    }
Josef E.
  • 2,179
  • 4
  • 15
  • 30
user3681220
  • 41
  • 1
  • 1
  • 3
  • Follow the error message's advice: Make your `main` method `public`. It currently has no access modifier. – rgettman May 27 '14 at 20:10

3 Answers3

8

When a method don't have a visibility it's package by default. Read here too.

So you should make your

static void main(String[] args)

public.

public static void main(String[] args)

The reason that the compiler don't complain about the public is because it doesn't care about the main method. It doesn't take difference for it if a main exists or not.

It's the JVM which need a start point public and static to launch your application.

Try to make other methods named main and you will see you don't have problems. main is a name like others.

Community
  • 1
  • 1
Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
1

The signature for a method to be used as an entry point for a java application is public static void main(String[] args).

But if the class isn't meant to be used as entry point, static void main(String[] args) is also a valid method signature - so the compiler doesn't complain.

The compiler doesn't know that you want to use the method as the entry point.

mschenk74
  • 3,561
  • 1
  • 21
  • 34
0

Your main method must be declared as public so that it can be accessed by the JVM (Java Virtual Machine) that actually executes the Java bytecode. Java bytecode is what results from compiling the code you write.

The JVM is programmed to look for a specific signature for the main method before executing the program. It finds this signature by looking for a specific series of bytes in the bytecode. That signature only results from compiling a method that is - public static void main. If any of the three modifiers (public, static and void) are omitted the bytecode code will look different and the JVM will not be able to find your main method.

You would not find this error when you compile the program because technically there is nothing illegal about having a method called "main." Main is not a reserved word in Java so there's nothing stopping you from using it as a method (or variable) title. The error will only be detected at runtime when the JVM actually tries to execute the code and cannot find a main method with the appropriate signature.

Rarw
  • 7,645
  • 3
  • 28
  • 46
  • this doesn't answer the part of the question why the compiler doesn't complain about it. – mschenk74 May 27 '14 at 20:27
  • The question is why do I get the error not why is it builds and fails at runtime. However, I will add something in just because you mentioned it. – Rarw May 27 '14 at 21:41