如何在Java中调用公共静态void main(String[]args)类内部的方法



我正在尝试加载一个json文件,将其转换为ConcurrentHashMap,然后用以下代码写入csv文件:

我的json文件的格式是

{"lemmas":{"doc4":"这可能会使它出错","doc3":"并且没有脏数据","doc2":"每个长度不同","doc1":"你应该发现它有五行","文档0":"这个是一个简单的文本文件">

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,%dn", 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((有两个参数,我不知道如何从ConcurrentHashMap<字符串,整数>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<字符串,整数>newhashmap=newobj。CountWordsInCorpus((';当然,它给出了一个错误"类型BDscriptiveStatistics中的方法CountWordsInCorpus(ConcurrentHashMap<String,String>("不适用于参数((。

我该怎么做?

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

这行很好;但它回来了。除了,CountWordsInCorpus方法有一个参数:引理。你需要在某个地方找到这些狐猴,并将它们传递给这个方法。您的StartCreatingStatistics方法会执行此操作,但它会调用CountWordsInCorpus,并将结果丢弃在垃圾中。

也许StartCreatingStatistics应该返回它。现在,main可以调用StartCreatingStatistics,保存它返回的内容,并将其传递给OutputCountsAsCSV

最新更新