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

Time:08-10

I'm trying to load a json file, convert it into a ConcurrentHashMap and then write into a csv file with the following code:

My json file is of the form

{"lemmas":{"doc4":"which might make it go wrong","doc3":"and no dirty datum","doc2":"each of vary length","doc1":"you should find that it have five line","doc0":"this be a simple text file"}}

    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...");
    
                    }
                }
            }
        }

I now want to call the OutputCountsAsCSV() method to pass the csv file name to it, say 'my_file.csv'.

I am not sure how to do it in the main(String[] args) method. It is easy to call StartCreatingStatistics(), for example, because there is only one argument, but OutputCountsAsCSV() has two arguments and I do not know how to pass ‘counts’ from ConcurrentHashMap<String, Integer> CountWordsInCorpus() to it as the first argument?

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

So if I try 'ConcurrentHashMap<String, Integer> newhashmap = newobj.CountWordsInCorpus()'; it of course, gives an error 'the method CountWordsInCorpus(ConcurrentHashMap<String, String>)' in the type BDescriptiveStatistics is not applicable for the arguments()'.

how can I do it please?

CodePudding user response:

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

This line is fine; but it back. Except, the CountWordsInCorpus method has an argument: lemmas. You need to find these lemmas someplace and pass them to this method. Your StartCreatingStatistics method does this, but it calls CountWordsInCorpus, and tosses the result in the garbage.

Perhaps StartCreatingStatistics should return it instead. Now your main can call StartCreatingStatistics, save what it returns, and pass that to OutputCountsAsCSV.

  • Related