字符份额百分比

Character share in %

public static void main(String[] args) throws Exception {
    Map<Character, Integer> characterCounter = new HashMap<Character, Integer>();
    File file = new File("/Users/Downloads/text.txt`enter code here`");
    try(Scanner s = new Scanner(file, "utf-8")){
        while (s.hasNext()) {
            char[] chars = s.nextLine().toLowerCase().toCharArray();
            for (Character c : chars) {
                if(!Character.isLetter(c)){
                    continue;
                }
                else if (characterCounter.containsKey(c)) {
                    characterCounter.put(c, characterCounter.get(c) + 1);
                } else {
                    characterCounter.put(c, 1);
                }
            }
        }
        for (Map.Entry<Character, Integer> countedArray : characterCounter.entrySet()) {
            System.out.println(countedArray.getKey() + ": " + countedArray.getValue());
        }
    }
}

以下代码打印给定文本中英文字母表中的每个字母出现了多少次file.For现在okay.Everything正在工作flawlessly.When我运行我明白了'a' 'b' etc.But 我想实现一点 more.I 希望它显示的不是字母出现的次数而是它们在文本中的百分比.比方说 'a' : 30 % 'b' - 12 % 等 text.I 我不确定我该如何实现。我正在寻找这样做的想法而不是现成的解决方案,因为我觉得它很简单,但我就是无法破解它。

public static void main(String[] args) throws Exception {
    Map<Character, Integer> characterCounter = new HashMap<Character, Integer>();
    File file = new File("/Users/Downloads/text.txt`enter code here`");
    int totalCount = 0;
    try(Scanner s = new Scanner(file, "utf-8")){
        while (s.hasNext()) {
            char[] chars = s.nextLine().toLowerCase().toCharArray();
            for (Character c : chars) {
                totalCount = totalCount + 1;
                if(!Character.isLetter(c)){
                    continue;
                }
                else if (characterCounter.containsKey(c)) {
                    characterCounter.put(c, characterCounter.get(c) + 1);
                } else {
                    characterCounter.put(c, 1);
                }
            }
        }
        for (Map.Entry<Character, Integer> countedArray : characterCounter.entrySet()) {
            System.out.println(countedArray.getKey() + ": " + countedArray.getValue() + " - " + ((double) countedArray.getValue()/ (double) totalCount));        }
    }
}