【问题标题】:Fix the array type and method error in my java classes, really lost at how to fix the error修复我的java类中的数组类型和方法错误,真的迷失了如何修复错误
【发布时间】:2016-07-21 06:35:43
【问题描述】:

运行解析器程序时出现以下错误,

Error: Main method not found in class TfIdfMain, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

同样在我的main方法中,有这样的错误,但是我已经在解析器类中创建了parserfile错误,

Multiple markers at this line
    - Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit 
     constructor
    - Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an 
     explicit constructor

同样在我的解析器类中,数组列表行周围有一个错误,它说数组无法解析,我应该如何解决这个问题?创建一个新变量。

这是我的两个主要类涉及的错误:

import java.io.FileNotFoundException;
import java.io.IOException;
public class TfIdfMain {

    }
   // public static void main(String args[]) throws FileNotFoundException, IOException {
   //     DocumentParser dp = new DocumentParser();
   //     dp.parseFiles("C:\\Users\\Sarah\\Documents");
   //     dp.getCosineMatrix(); 
   // }
  }
}

我的文档解析器类:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;

public class DocumentParser {

    private void doSomething(){
            String text = "Professor, engineering, data, mining, research";
            StringTokenizer str = new StringTokenizer(text);
            String word[] = new String[10];
            String unique[] = new String[10];
            String x;
            int count = -1;
            while (str.hasMoreTokens()) {
                count++;
                x = str.nextToken();

            if (f.getName().endsWith(".txt")) {
                in = new BufferedReader(new FileReader(f));
                StringBuilder sb = new StringBuilder();
                String s = null;
                while ((s = in.readLine()) != null) {
                    sb.append(s);
                }
                String[] tokenizedTerms = sb.toString().replaceAll("[\\W&&[^\\s]]", "").split("\\W+");   //to get individual terms
                for (String term : tokenizedTerms) {
                    if (!allTerms.contains(term)) {  
                        allTerms.add(term);
                    }
                }
                termsDocsArray.add(tokenizedTerms);
            }
        }

    }

    public void tfIdfCalculator() {
        double tf; 
        double idf; 
        double tfidf;       
        for (String[] docTermsArray : termsDocsArray) {
            double[] tfidfvectors = new double[allTerms.size()];
            int count = 0;
            for (String terms : allTerms) {
                tf = new TfIdf().getTf(docTermsArray, terms);
                idf = new TfIdf().idfCalculation(termsDocsArray, terms);
                tfidf = tf * idf;
                tfidfvectors[count] = tfidf;
                count++;
            }
            tfidfDocsVector.add(tfidfvectors);      
        }
    }
    public void getCosineMatrix() {
        for (int i = 0; i < tfidfDocsVector.size(); i++) {
            for (int j = 0; j < tfidfDocsVector.size(); j++) {
                System.out.println("between " + i + " and " + j + "  =  "
                                   + new CosineSimilarity().getCosine
                                       (
                                         tfidfDocsVector.get(i), 
                                         tfidfDocsVector.get(j)
                                       )
                                  );
            }
        }
    }
}

【问题讨论】:

    标签: java arrays parsing compiler-errors runtime-error


    【解决方案1】:

    阅读您的错误信息,然后检查您的代码:

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

    可能是什么问题?找不到主要方法。在您的代码中,它被注释掉了。

    在您的TfIdfMain-class 中: 至少你的for循环必须在方法/构造函数中。 做类似的事情

    public class TfIdfMain
        public TfIdfMain(){
            for(String file : files) {
                DocumentParser dp = new DocumentParser();
                dp.parseFiles(file);
                dp.getCosineMatrix();
            }
        }
    }
    

    【讨论】:

    • 谢谢,我给你发了邮件。您现在可以删除您的电子邮件评论。
    【解决方案2】:

    您的主要方法已被注释掉,这就是编译器找不到它的原因。 for 循环不在方法内部,而仅在类主体中。这在java中是错误的。 编译器的错误信息会告诉你具体做什么,你必须提供一个可以处理异常的特殊构造函数。

    【讨论】:

    • 非常感谢!我已经在这段代码上工作了几个小时了!因为我不能在另一个小时内发布另一个问题,而且我不知道你是否会稍后。我正在努力让这段代码正常工作。我可以给你发电子邮件吗?我的邮箱是gmail上的firstjavapython,这是一个临时账号,所以你不用担心隐私问题。请考虑帮助我!我真的需要你的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    相关资源
    最近更新 更多