-
Notifications
You must be signed in to change notification settings - Fork 0
/
c11test20.java
32 lines (32 loc) · 1.31 KB
/
c11test20.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 第11章 练习20
// @author 晴和羽织
import java.util.*;
import lyutil.*;
import static lyprint.Print.*;
public class c11test20 {
private final static Set<Character> vowels = new HashSet<Character>(Arrays.asList('a','e','i','o','u','A','E','I','O','U'));
static void updateStat(Map<Character, Integer> stat, char letter) {
Character ch = Character.toLowerCase(letter);
Integer freq = stat.get(ch);
stat.put(ch, freq == null ? 1 : freq + 1);
}
public static void main(String[] args) {
HashMap<Character, Integer> fileStat = new HashMap<Character, Integer>();
HashSet<String> processWords = new HashSet<String>();
HashMap<Character, Integer> wordStat = new HashMap<Character, Integer>();
for(String word: new TextFile("javacoding\\AbstractInstrument.java","\\W+")) {
wordStat.clear();
for(char letter: word.toCharArray()){
if(vowels.contains(letter)){
updateStat(wordStat,letter);
updateStat(fileStat,letter);
}
}
if(!processWords.contains(word)) {
processWords.add(word);
printl("Vowels in [" + word + "]: " + wordStat);
}
}
printl("文件中所有元音字母:" + fileStat);
}
}