簡體   English   中英

如何在 Java 中調用公共 static void main(String[] args) class 中的方法?

[英]How to call a method inside the public static void main(String[] args) class in Java?

我正在嘗試加載 json 文件,將其轉換為 ConcurrentHashMap,然后使用以下代碼寫入 csv 文件:

我的 json 文件的格式為

{"lemmas":{"doc4":"這可能導致 go 錯誤","doc3":"並且沒有臟數據","doc2":"每個不同的長度","doc1":"你應該發現它有五行","doc0":"這是一個簡單的文本文件"}}

    package pipeline;
    
    import java.io.FileWriter;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Map.Entry;
    import java.util.concurrent.ConcurrentHashMap;
    
    import helpers.JSONIOHelper;
    
    public class DescriptiveStatistics {
    
        private static void StartCreatingStatistics(String filePath) {
            System.out.println("Loading file...");
    
            JSONIOHelper JSONIO = new JSONIOHelper(); // create an object of the JSONIOHelper class
            JSONIO.LoadJSON(filePath); // call the LoadJSON method
            ConcurrentHashMap<String, String> lemmas = JSONIO.GetLemmasFromJSONStructure();
            
    
            lemmas.forEach((k, v) -> System.out.printf("    %s%n", v));
    
            CountWordsInCorpus(lemmas); // call this method from the end of the StartCreatingStatistics()
        }
    
        private static ConcurrentHashMap<String, Integer> CountWordsInCorpus(ConcurrentHashMap<String, String> lemmas) {
    
            // compile the words in the corpus into a list
            ArrayList<String> corpus = new ArrayList<String>();
            // store the words together with their frequencies
            ConcurrentHashMap<String, Integer> counts = new ConcurrentHashMap<String, Integer>();
        
            for (Entry<String, String> entry : lemmas.entrySet()) {
    
                for (String word : entry.getValue().split(" ")) {
    
                    corpus.add(word);
    
                }
            }

   // getting words and their frequencies 
            for (String word : corpus) {
                if (counts.containsKey(word)) {
                    counts.put(word, counts.get(word) + 1);
                } else {
                    counts.put(word, 1);
    
                }
            
            }
                                
            return counts;
        }
    // writing into a csv file
        private void OutputCountsAsCSV(ConcurrentHashMap<String, Integer> counts, String filename) {
            String CSVOutput = new String("");
    
            for (Entry<String, Integer> entry : counts.entrySet()) {
                String rowText = String.format("%s,%d\n", entry.getKey(), entry.getValue());
    
                System.out.println(rowText);
                CSVOutput += rowText;
                System.out.println(CSVOutput);
    
                {
                    try (FileWriter writer = new FileWriter(filename)) {
                        writer.write(CSVOutput);
                        System.out.println("CSV File saved successfully...");
                    }
    
                    catch (Exception e)
    
                    {
                        System.out.println("Saving CSV to file failed...");
    
                    }
                }
            }
        }

我現在想調用 OutputCountsAsCSV() 方法將 csv 文件名傳遞給它,比如“my_file.csv”。

我不確定如何在 main(String[] args) 方法中執行此操作。 例如,調用 StartCreatingStatistics() 很容易,因為只有一個參數,但 OutputCountsAsCSV() 有兩個 arguments,我不知道如何將 ConcurrentHashMap<String, Integer> CountWordsInCorpus() 中的“計數”傳遞給它第一個論點?

public static void main(String[] args) {
    
    String filePath = "JSON_simple.json";       
    DescriptiveStatistics newobj = new 
DescriptiveStatistics();
    newobj.StartCreatingStatistics(filePath);
    ...
    // ConcurrentHashMap<String, Integer> newhashmap = 
//newobj.CountWordsInCorpus()
    String filename = "my_file.csv";      
    OutputCountsAsCSV ( newhashmap, filename);
}

因此,如果我嘗試 'ConcurrentHashMap<String, Integer> newhashmap = newobj.CountWordsInCorpus()'; 它當然會給出錯誤“BDescriptiveStatistics 類型的方法 CountWordsInCorpus(ConcurrentHashMap<String, String>)”不適用於 arguments()”。

請問我該怎么做?

ConcurrentHashMap<String, Integer> newhashmap = newobj.CountWordsInCorpus()

這條線很好; 但它回來了。 除了, CountWordsInCorpus方法有一個參數:引理。 您需要在某個地方找到這些引理並將它們傳遞給此方法。 您的StartCreatingStatistics方法執行此操作,但它調用CountWordsInCorpus並將結果扔進垃圾箱。

也許StartCreatingStatistics應該返回它。 現在您的 main 可以調用StartCreatingStatistics ,保存它返回的內容,並將其傳遞給OutputCountsAsCSV

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM