0

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?

  • It is possible. You simply must compute the lengths of all relevant words and filenames and then print it all out with the relevant spaces in between. If HTML is okay you could write a html-file and use a table there. That would be easier to format. – Ralf Renz May 24 '18 at 09:53
  • Check the following post: [link](https://stackoverflow.com/questions/2745206/output-in-a-table-format-in-javas-system-out) – J Robes May 24 '18 at 09:57
  • @RalfRenz html is okay, but have you some example? – Arnas Arnelis May 24 '18 at 11:54
  • @JRobes thank you. I couldn't find where to write all tale to the file – Arnas Arnelis May 24 '18 at 11:55

1 Answers1

1

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) {

    }
}
Ralf Renz
  • 1,061
  • 5
  • 7
  • Thx, but this one is not reading from file right? :) I would like that this code would get all info from the file :) and how I could make it to the servlet? @RalfRenz – Arnas Arnelis May 24 '18 at 15:34
  • I think you solved the first part - how to get the relevant infos from the file(s). Since your method getWordCount() was not in your sourcecode I didn't recreated it and simply filled the relevant structure directly. – Ralf Renz May 25 '18 at 06:50
  • Oh i understood :) but how to make your code to get data about last uploaded files? Should i put here my code? – Arnas Arnelis May 26 '18 at 05:27
  • My method writeHtmlFiles takes a Map with filenames as keys and the corresponding wordmaps as value. How you fill the structure is up to you. In your code you just create the wordmap for one file Hamletas.txt. This would be one Entry in my data structure. If you can do it for one file you could possibly do it for others. – Ralf Renz May 28 '18 at 08:32