Arnas Arnelis
Arnas Arnelis

Reputation: 83

How to write to file the output from program like a matrix, Java

I am trying to count duplicating words in a file and I would like to write to the file my output

   public static void main(String a[]){
    MaxDuplicateWordCount mdc = new MaxDuplicateWordCount();
    Map<String, Integer> wordMap = mdc.getWordCount("C:/Users/Arnoldas/Desktop/Hamletas.txt");
    List<Entry<String, Integer>> list = mdc.sortByValue(wordMap);
    for(Map.Entry<String, Integer> entry:list){
        System.out.println(entry.getKey()+" ==== "+entry.getValue());
    }
}

Output now looks like:

hamletui ==== 4
šmėkla ==== 2
jo ==== 2
hamletas ==== 1
danijos ==== 1
pagrindiniam ==== 1
mirusio ==== 1
princui ==== 1
herojui, ==== 1
apsireiškusi ==== 1
karalystės ==== 1
paveda ==== 1
parketas ==== 1
herojui ==== 1
žudikui ==== 1
neseniai ==== 1
omletas ==== 1
atkeršyti ==== 1
tėvo ==== 1

I would like to put this output to the file and it should look like:

               | hamletui | šmėkla | jo  | hamletas | danijos | pagrindiniam |   ...
-------------------------------------------------------------------------------------
Hamletas.txt   |     4    |    2   |  2  |    0     |    0    |      0       |   ...
-------------------------------------------------------------------------------------
Other_file.txt |     0    |    0   |  6  |    3     |    4    |      1       |   ...

Is it possible to do like that? Also I would like to get output and from others files. Have any ideas?

Upvotes: 0

Views: 51

Answers (1)

Ralf Renz
Ralf Renz

Reputation: 1061

OK. An example how to write it as HTML-File with use of a table. Input is a map with filenames as key and a map of words with counts. First it looks which words are mentioned in the maps. Then it writes HTML. The first table row contains all those words, after that for each filename a row is created with the count of the words.

public static void main(String a[]) {
    Map<String, Map<String, Integer>> wordMaps = new HashMap<>();

    wordMaps.put("Hamletas.txt", new HashMap<String, Integer>());
    wordMaps.get("Hamletas.txt").put("hamletui", 4);
    wordMaps.get("Hamletas.txt").put("šmėkla", 2);
    wordMaps.get("Hamletas.txt").put("jo", 2);

    wordMaps.put("Other_file.txt", new HashMap<String, Integer>());
    wordMaps.get("Other_file.txt").put("jo", 6);
    wordMaps.get("Other_file.txt").put("hamletas", 3);
    wordMaps.get("Other_file.txt").put("danijos", 4);
    wordMaps.get("Other_file.txt").put("pagrindiniam", 1);

    writeHtmlFile("example.html", wordMaps);
}

public static void writeHtmlFile(String filename, Map<String, Map<String, Integer>> wordMaps) {
    // get all words that are duplicates
    Set<String> words = new HashSet<>();
    for (Map<String, Integer> map : wordMaps.values()) {
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() > 1) {
                words.add(entry.getKey());
            }
        }
    }

    try (PrintWriter out = new PrintWriter(new FileWriter(filename))) {
        out.println("<!DOCTYPE html><html>");
        out.println("<head><meta charset=\"utf-8\"/></head");
        out.println("<body>");
        out.println("<table border=\"1\">");

        // write Headline
        out.println("<tr>");
        out.println("<td>&#160;</td>"); // empty cell
        for (String word : words) {
            out.println("<td>" + word + "</td>"); // empty cell
        }
        out.println("</tr>");

        // write for all files
        for (Map.Entry<String, Map<String, Integer>> fileWordMap : wordMaps.entrySet()) {
            out.println("<tr>");
            out.println("<td>" + fileWordMap.getKey() + "</td>"); // filename
            Map<String, Integer> wordMap = fileWordMap.getValue();
            for (String word : words) {
                out.println("<td align=\"right\">" + (wordMap.containsKey(word) ? wordMap.get(word) : 0) + "</td>"); // number per
                                                                                                     // word or 0
            }
            out.println("</tr>");
        }

        out.println("</table>");
        out.println("</body>");
        out.println("</html>");
    } catch (IOException e) {

    }
}

Upvotes: 1

Related Questions