首页 文章

错误:类MovieDatabase中找不到主要方法[重复]

提问于
浏览
2

这个问题在这里已有答案:

错误:在MovieDatabase类中找不到主方法,请将main方法定义为:public static void main(String [] args)或JavaFX应用程序类必须扩展javafx.application.Application

import java.io.FileInputStream;
import java.util.Scanner;
import java.util.Arrays;

public class MovieDatabase {
    private int[] analysis;

    //creating the contructor
    public MovieDatabase(String file){
        analysis = new int[2015];

        this.load(file);
    }
        //uses the load(String file) method from downstairs to do all of the work


    public void  load(String file){
        Scanner theScanner = null;

        try{
            //inputing the into the scanner
            theScanner = new Scanner(new FileInputStream(file));
        }
        catch(Exception ex){ 
            ex.printStackTrace();
        }
        // as long as the scanner has another line 
        while(theScanner.hasNextLine())
        {
            String Line = theScanner.nextLine();
            //make an array called split and allocated different elements based on a seperation of ##
            String split[] = Line.split("##");
            int year = Integer.valueOf(split[1]); 
            analysis[year] ++;
        }   

    }

    //print out the array in the synchronous format
    public void print(){
        System.out.printf("%1$-30s %2$10s %3$10s %4$10s ", "Year", "Occurances", "", "");
        //go through the array
        for (int i =0;i < analysis.length ;i++ ) {
            if(analysis[i] >0){
                for (int j =i;j < analysis.length ;i++ ){
                    System.out.printf("%1$-30s %2$10s %3$10s %4$10s ", j, analysis[j], "", "");
                }
            }   
        }
    }
}

如何修复此错误消息?我读过其他类似的问题,但只是说让公开课 . 我是公开的 .

4 回答

  • 0

    Java中的 main() 方法是一种标准方法,JVM使用它来开始执行任何Java程序 . main方法被称为Java应用程序的入口点,在核心java应用程序的情况下也是如此

    你错过了它 . 添加以下 main() 方法

    public static void main(String[] args) {
        MovieDatabase db = new MovieDatabase("file/path/goes/here");
        db.print();
    }
    

    在Java编程语言中,每个应用程序都必须包含一个main方法,其签名为:public static void main(String [] args)

  • 0

    正如错误所暗示的,如果它的非FX项目,只需定义如下:

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

    或者更改类定义,以便扩展Application,例如:

    public class MovieDatabase extends Application
    
  • 0

    要调用任何应用程序,JVM需要 main() 方法,并且应具有以下访问说明符和修饰符,

    public static void main(String args[])
    

    public - 所有人都应该可以访问它
    static - JVM无法创建类的实例,因此方法应为 static
    void - 方法没有返回任何内容

    对于每个java应用程序,main方法都是入口点,因此您的应用程序应该至少有一个主要方法来开始执行应用程序 .

  • 1

    这是因为你忘了添加主要方法 . 错误清楚地说明:

    请将main方法定义为:public static void main(String [] args)

    所以添加主要:

    public static void main(String[] args) {
        MovieDatabase m = new MovieDatabase("Your File Path");
        m.print();
    }
    

相关问题