简体   繁体   中英

How to fix the following error: “Error: Main method not found in class”

I'm essentially new to Java and for fun I'm creating a Stock Market Simulator/Game. I know my code isn't "perfect" but remember I'm new and i have 1 error.

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

I've looked at all the argument and parameter tutorials I could find and searched StackOverflow's Questions and couldn't find a solution.

If you can, can someone come up with a way to "Delete/Fix" the error, or just help me out as much as you can. Thanks in Advance!

My Code:

package StockPackage;

import java.util.Scanner;    

public class Dog {      

    public static void main(String[] args, int CurMoney, String[] inputStocks, 
        int[] StockMoney) {
        Dog Run = new Dog();
        Run.MainScript();
        Run.OptionStartingMethod(inputStocks, CurMoney, StockMoney);
    }     

//==============================================================================
//                          ||Objects||
    void options(String[] inputStocks, int CurMoney, int[] StockMoney) {
        String optionEXIT, optionStartStock, optionBuyStock, optionSellStock;
        String optionCheat, optionInfo, optionCredits, optionClose, OptionInput;
        int i, j, k;            

        Scanner optionInput = new Scanner(System.in);

        Dog FlixSibling = new Dog();            

        System.out.println("=============| Options | =============");
        System.out.println(" optionEXIT       " + " optionStartStock   " + "optionBuyStock");
        System.out.println(" optionSellStock  " + " optionCheat   " + "     OptionInfo  ");
        System.out.println(" optionCredits    " + " optionClose   " + "        optionCheckCash");
        System.out.println();
        System.out.print("==|  ");

        OptionInput = optionInput.nextLine();

        if(OptionInput.equals("optionEXIT")) {
            FlixSibling.Close();
        } else if(OptionInput.equals("optionStartStock")) {

            StartStock(inputStocks, StockMoney, CurMoney);
        } else if(OptionInput.equals("optionBuyStock")) {

        } else if(OptionInput.equals("optionSellStock")) {

        } else if(OptionInput.equals("optionCheat")) {

        } else if(OptionInput.equals("optionInfo")) {

        } else if(OptionInput.equals("optionCredits")) {

        } else if(OptionInput.equals("optionClose")) {
            optionClose1(inputStocks, CurMoney, StockMoney);
        }

    }
//==============================================================================
//                        ||Option Holders||
    void optionClose1(String[] inputStocks, int CurMoney, int[] StockMoney) {
        int i, j, k;
        for(i = 0; i <= 25; i++) {
            System.out.println();
        }
        MainScript();
        for(i = 0; i <= 3; i++) {
            System.out.println();
        }
    }
    void OptionStartingMethod(String[] inputStocks, int CurMoney, int[] StockMoney) {
        Scanner InputBoard1 = new Scanner(System.in);

        String Options;
        System.out.println();
        System.out.println("Option: Press 'P'");

        Options = InputBoard1.nextLine();

        if(Options.equals("P")) {
            options(inputStocks, CurMoney, StockMoney);
        }
    }
//==============================================================================
//                      ||StartStock Method||
    void StartStock(String[] inputStocks, int[] StockMoney, int CurMoney) {
        Dog StartStocking = new Dog();

        String Answer;
        String name, price;
        int CurCash;

        Scanner StartingStockInput = new Scanner(System.in);

        System.out.println();
        System.out.println("Do you Wish to create a Stock? (y/n)");
        Answer = StartingStockInput.nextLine();

        if(Answer.equals("y")) {
            System.out.println("$15000 Has been taken out of your Account");
            CurMoney -= 15000;    // for Creating the Stock

            System.out.println("Name|  ");
            name = StartingStockInput.nextLine();

            System.out.println("input Amount of Cash|  ");
            CurCash = StartingStockInput.nextInt();
            if(CurCash > CurMoney) {
                CurCash = CurMoney;
            }
            MainScript();


        } else if(Answer.equals("n")) {
            optionClose1(inputStocks, CurMoney, StockMoney);
        }

    }


//==============================================================================
//                        ||Close Method||
    void Close() {
        System.out.println();
        System.out.println();
        System.out.println("You Exit'ed out of Stock market SIM");
    }

//==============================================================================
//                        ||Main Method||
    void MainScript() {

        System.out.println("Welcome to the Stock Market");

                //        ||Variables||
        int StockPercent;
        int CurMoney;
        String Options;
        CurMoney = 64000;
        String Place, Place1, Place2;
        Place = "0";
        Place1 = ",000";
        Place2 = ",000,000";
        String nothing = "|Under Construction|";
        int i, j, k;

        Dog Flix = new Dog();

        boolean Construction = false;

        int[] StockMoney;
        StockMoney = new int[500];

        String[] inputStocks;
        inputStocks = new String[200]; // Used for storing the inputed Stocks
                                       // 50 And Above are used for Creating Stocks
        String per = "%";

        inputStocks[0] = "Google"; 
        StockMoney[50] = 0; 
        StockMoney[0] = 400000000;     // Using the 2 bit 000's for the millions place;

        inputStocks[2] = "MicroSoft";
        StockMoney[51] = 0;
        StockMoney[1] = 250000000;

        inputStocks[4] = "Yahoo";
        StockMoney[52] = 0;
        StockMoney[2] = 100000000;

        inputStocks[6] = "Cheeez It's";
        StockMoney[53] = 0; 
        StockMoney[3] = 50000;




                //         ||input From User||
        Scanner InputBoard = new Scanner(System.in);



                //         ||Output From Program||

        System.out.println("[]---------------| Stocks |---------------[]");
        System.out.println(inputStocks[0] + "      " + StockMoney[0] + "    " + StockMoney[50] + per);
        System.out.println(inputStocks[2] + "   " + StockMoney[1] + "    " + StockMoney[51] + per);
        System.out.println(inputStocks[4] + "       " + StockMoney[2] + "    " + StockMoney[52] + per);
        System.out.println(inputStocks[6] + " " + StockMoney[3] + "        " + StockMoney[53] + per);
        System.out.println();
        System.out.println();



        if(Construction) {
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
        }
    }


}

Your main method does not match the signature of a standard main method, that normally starts a Java application. Yours has way too many parameters and should have only one parameter, an array of String.

So you must change this:

// incorrect main method signature
public static void main(String[] args, int CurMoney, String[] inputStocks, int[] StockMoney) {

to this:

// correct main method signature
public static void main(String[] args) {

and get all the information needed in some way, perhaps from a file?

JVM only knows the following format of main method,

public static void main(String[] args)
{
}

the main keyword is reserved so you can't use it for any other purpose like multiple parameters and so on.

You can't change the public static void main method so you have tu put in correct way

    public static void main(String[] args) {

        int CurMoney;//fill the varible
        String[] inputStocks;//fill the Array
        int[] StockMoney;//fill the Array
        Dog Run = new Dog();
        Run.MainScript();
        Run.OptionStartingMethod(inputStocks, CurMoney, StockMoney);
    }

rest of the code you have...   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM