我試圖在Eclipse中運行下面的簡單字計數程序,我得到一個錯誤。我檢查了我的配置在運行方式和它們是正確的主要方法錯誤的Java
Error: Main method not found in class wordcount.WordCount, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
這裏程序
package wordcount;
import java.io.*;
import java.util.*;
// This program does word-counting on the text of Moby Dick.
public class WordCount {
public static void main(String[] args) throws FileNotFoundException {
HashMap map = new HashMap(); // word --> # of occurrences
// read each word from the file
Scanner in = new Scanner(new File("mobydick.txt"));
while (in.hasNext()) {
String word = in.next();
if(map.containsKey(word)) {
// if we have already seen this word before,
// increment its count by one
Integer count = (Integer)map.get(word);
map.put(word, new Integer(count.intValue() + 1));
} else {
// we haven't seen this word, so add it with count of 1
map.put(word, new Integer(1));
}
}
// now print out every word in the book, along with its count,
// in alphabetical order
ArrayList arraylist = new ArrayList(map.keySet());
Collections.sort(arraylist);
for (int i = 0; i < arraylist.size(); i++) {
String key = (String)arraylist.get(i);
Integer count = (Integer)map.get(key);
System.out.println(key + " --> " + count);
}
}
}
對我來說工作正常...檢查你的構建路徑 – chenchuk
爲什麼你的主要方法拋出異常?你認爲你把它扔到哪裏?你的主要方法是處理它的最後機會。你的代碼將運行得很好。你確定你保存了代碼嗎? – Stultuske
@chenchuk如何檢查構建路徑? –