Java program to count characters in text file.
Output
Total characters: 671751 : 111015 16.526212837792574% !: 499 0.07428347706218524% ': 741 0.11030873046709272% (: 18 0.0026795643028443574% ): 18 0.0026795643028443574% *: 30 0.0044659405047405955% ,: 9132 1.3594322896430373% -: 1118 0.16643071614333288% .: 6180 0.9199837439765627% 0: 6 8.931881009481192E-4% 1: 19 0.0028284289863357106% 2: 18 0.0026795643028443574% 3: 16 0.002381834935861651% 4: 16 0.002381834935861651% 5: 17 0.002530699619353004% 6: 10 0.0014886468349135321% 7: 6 8.931881009481192E-4% 8: 7 0.0010420527844394725% 9: 6 8.931881009481192E-4% :: 132 0.019650138220858624% ;: 1538 0.22895388320970123% ?: 462 0.06877548377300517% A: 460 0.06847775440602247% B: 1078 0.16047612880367876% C: 629 0.09363588591606116% D: 548 0.08157784655326154% E: 731 0.10882008363217918% F: 146 0.021734243789737567% G: 173 0.0257535902440041% H: 676 0.10063252604015475% I: 2576 0.38347542467372586% J: 322 0.04793442808421573% K: 95 0.014142144931678554% L: 735 0.1094155423661446% M: 1702 0.25336769130228315% N: 238 0.03542979467094206% O: 184 0.02739110176240899% P: 178 0.026497913661460867% R: 105 0.015630791766592086% S: 512 0.07621871794757283% T: 778 0.11581672375627278% U: 23 0.003423887720301124% V: 19 0.0028284289863357106% W: 637 0.09482680338399198% Y: 351 0.05225150390546497% Z: 5 7.443234174567661E-4% _: 808 0.12028266426101339% a: 41225 6.136946576931035% b: 8010 1.1924061147657392% c: 12833 1.9103804832445355% d: 21754 3.238402324670897% e: 68641 10.218220739529976% f: 11854 1.764641958106501% g: 9857 1.4673591851742684% h: 33391 4.970740646459775% i: 35256 5.248373281171148% j: 551 0.08202444060373561% k: 3113 0.4634157597085825% l: 20857 3.1048707035791536% m: 13062 1.9444704957640553% n: 37452 5.575280126118161% o: 39855 5.933001960547882% p: 8049 1.198211837421902% q: 627 0.09333815654907845% r: 32193 4.792400755637134% s: 32598 4.852690952451131% t: 45865 6.8276787083309145% u: 14964 2.2276111237646092% v: 5707 0.8495707486851526% w: 11668 1.7369531269771092% x: 839 0.12489746944924533% y: 12355 1.8392231645356687% z: 931 0.13859302033044982% ?: 1791 0.26661664813301356% ?: 1740 0.25902454927495455%
It was generated for “Pride And Prejudice” without header and license.
Main.java
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; // ? 2018 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net public class Main { public static void main(String[] args) { Statistics statistics = new Statistics(); try (Stream<String> stream = Files.lines(Paths.get("PRIDE AND PREJUDICE.txt"))) { stream.forEach(statistics::process); } catch (IOException e) { e.printStackTrace(); } statistics.printStatistics(); } }
Statistics.java
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.TreeMap; // ? 2018 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net public class Statistics { private Map<Character, Integer> charactersOccurrences = new TreeMap<>(); private long characterCount; public void process(String text) { char charAt; for (int i = 0; i < text.length(); ++i) { charAt = text.charAt(i); if (charactersOccurrences.containsKey(charAt)) { charactersOccurrences.merge(charAt, 1, (oldValue, one) -> oldValue + one); } else { charactersOccurrences.put(charAt, 1); } ++characterCount; } } public void printStatistics() { List<Character> keys = new ArrayList<>(charactersOccurrences.keySet()); int value; System.out.println("Total characters: " + characterCount); for (int i = 0; i < keys.size(); ++i) { value = charactersOccurrences.get(keys.get(i)); System.out.println(keys.get(i) + ": " + value + " " + ((double) value / characterCount) * 100.0d + "%"); } } }