2015-10-07 44 views
0

我試圖在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); 
    } 

    } 
} 
+0

對我來說工作正常...檢查你的構建路徑 – chenchuk

+1

爲什麼你的主要方法拋出異常?你認爲你把它扔到哪裏?你的主要方法是處理它的最後機會。你的代碼將運行得很好。你確定你保存了代碼嗎? – Stultuske

+0

@chenchuk如何檢查構建路徑? –

回答

0

爲什麼你的主要方法拋出Exception?你認爲你把它扔到哪裏?你的主要方法是處理它的最後機會。

你的代碼運行的很好。你確定你保存了代碼嗎?如果沒有,則代碼不會被編譯,因此無法找到該方法。

0

當您在IDE中按RUN按鈕時,它會嘗試運行上次使用的項目/類,因此可能不會在單擊RUN後啓動此類。附:對不起英語不好。