package set;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Exercise21_04 {
public static void main(String[] args) throws IOException {
System.out.print("Enter a file: ");
File file = new File(new Scanner(System.in).nextLine());
if (!file.exists())
System.out.println("File " + file + " does not exist");
else if (!file.isFile())
System.out.println("Not a file");
else {
int[] count = countVowel(file);
System.out.println("The number of vowels and novowels is " + count[0] + " and " + count[1]);
}
}
/** 统计元音和辅音数目 */
public static int[] countVowel(File file) throws IOException {
Set<Character> set = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u')); //元音集合
int[] count = {0, 0}; //元音、辅音数目
try(Scanner input = new Scanner(file)) {
while (input.hasNextLine()) { //辩能力文件
String text = input.nextLine(); //获取行
for (char c: text.toCharArray()) { //遍历字符数组
char ch = Character.toLowerCase(c); //字符统一转换为小写
if (set.contains(ch)) //元音时++
count[0]++;
else if ('a' <= ch && ch <= 'z') //辅音时
count[1]++;
}
}
}
return count;
}
}