dan luo dan luo - 2 months ago 12
Java Question

Fix the array type and method error in my java classes, really lost at how to fix the error

I have the following error when I run my parser program,

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


Also in my main method, there is a error like this, but I already created the parserfile error in parser class,

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


Also in my parser class, there is an error around the array list line, it says array cannot be resolved, how should I fix this? Create a new variable.

Here are my two main classes involved the errors:

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();
// }
}
}


My document parser class:

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)
)
);
}
}
}
}

Answer

read your error-message and then check your code:

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

what could be the problem? the main-method was not found. In your code it is commented out.

And in your TfIdfMain-class: at least your for-loop has to be in a method / constructor. Do something like

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