繁体   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