私の課題では、ファイルを読み取り、各行の母音の数を出力するように求められています。そして、奇妙な理由で、いくつかの行では適切な量の母音を出力しますが、他の行では出力しません。なぜこれをしているのですか?
母音を計算する私の方法は次のとおりです。
public static int calculateVowels(String line)
{
char[] vowels = new char[] {'a','e','i','o','u'};
// count the number of vowels in a line
int vowelCount = 0;
for (int i = 0; i < line.length(); i++) {
char a = line.charAt(i);
for (char vowel : vowels) {
if (a == vowel){
vowelCount++;
break;
}
}
}
return vowelCount;
}
そして、これをメインで呼び出すと次のようになります。
while ((line = br2.readLine()) != null)
{
lineCount++;
// count the number of words in a line
String[] words = line.split(" ");
if (words != null)
wordCount += words.length;
int vowelCount = calculateVowels(line);
System.out.println("Line " + lineCount + " has " + vowelCount + " vowels.");
}
何か案は?!ありがとう!