【问题标题】:Cannot make a static reference to the non-static method error when writing ConcurrentHashMap to csv [duplicate]将 ConcurrentHashMap 写入 csv 时无法对非静态方法错误进行静态引用 [重复]
【发布时间】:2022-08-10 00:30:53
【问题描述】:

我正在尝试将 ConcurrentHashMap 写入 csv 文件,并收到此错误:

Exception in thread \"main\" java.lang.Error: Unresolved compilation problem: 
    Cannot make a static reference to the non-static method OutputCountsAsCSV(ConcurrentHashMap<String,Integer>, String) from the type BDescriptiveStatistics 

我有一个这样的json文件

{\"lemmas\":{\"doc4\":\"这可能会出错\",\"doc3\":\"并且没有脏数据\",\"doc2\":\"每个改变长度\",\"doc1\":\"你应该会发现它有五行\",\"doc0\":\"这是一个简单的文本文件\"}}

我希望 csv 文件看起来像这样:

datum,1
no,1
be,1
vary,1
...
it,2

所以我在我的主要方法中有这个问题 - 我不知道如何调用方法,私有静态 ConcurrentHashMap<String, Integer>CountWordsInCorpus(..) 它返回计数并将其用作 OutputCountsAsCSV() 写入的第一个参数一个csv文件。

这是我尝试过的。

    public static void main(String[] args) {

        StartCreatingStatistics(\"JSONDataStore_simple.json\");
        
//      BDescriptiveStatistics newobj = new BDescriptiveStatistics();
                    
//      newhashmap = newobj.CountWordsInCorpus(ConcurrentHashMap<String, String> lemmas);
        
        ConcurrentHashMap<String, Integer> newhashmap = new ConcurrentHashMap<String, Integer>();
        
        OutputCountsAsCSV ( newhashmap, \"csv_file.csv\");
    }

我知道我需要改为启动 BDescriptiveStatistics 类的对象。 如果我运行两个未注释的行,我会得到一个不同的错误,\'newhashmap 无法解析为变量\'。

我的其余代码如下:

    package pipeline;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Map.Entry;
    import java.util.concurrent.ConcurrentHashMap;
    
    import helpers.JSONIOHelper;
    
    public class BDescriptiveStatistics {
    
        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); 
        }

// This   method compiles the words and the frequency of each word and returns counts as a hash map.

        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);
    
                }
            }
    
            for (String word : corpus) {
                if (counts.containsKey(word)) {
                    counts.put(word, counts.get(word) + 1);
                } else {
                    counts.put(word, 1);
    
                }
            
            }
    
            for (String word : counts.keySet()) {
                String wordKey = word;
                Integer countsValue = counts.get(word);
    
                System.out.println(wordKey + \" | \" + countsValue);
            }
            
            return counts;   // its a ConcurrentHashMap<String, Integer> 
        }

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

谢谢你。

    标签: java csv concurrenthashmap


    【解决方案1】:

    您的 OutputCountsAsCSV 方法不是静态的,这意味着您需要一个 BDescriptiveStatistics 实例来运行调用它。

    static 添加到方法声明中,就像该类中的其他方法一样。

    Please search the site before asking a question next time

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 2013-03-31
      • 2014-11-19
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 2021-08-31
      相关资源
      最近更新 更多