次のようなwhileステートメントを実行すると、行カウンターを追加する方法がわかりません
while (fileReader.hasNextLine()) {
lines+=1;
file.nextLine();
}
その後、残りの母音、文などは 0 に設定されます。
私のコードは次のとおりです。
Scanner input = new Scanner(System. in );
System.out.println("Enter file name: ");
File file = new File(input.nextLine());
if (file.length() == 0) {
System.out.println("The input file is empty.");
System.exit(1);
}
Scanner fileReader = new Scanner(file);
while (fileReader.hasNext()) {
String word = fileReader.next();
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowels += 1;
if ((ch == '!' || ch == '.' || ch == '?')) sentences += 1;
if (Character.isLetterOrDigit(ch)) alphaNumeric += 1;
switch (ch) {
case ',':
punctuation += 1;
break;
case '[':
punctuation += 1;
break;
case ']':
punctuation += 1;
break;
case ':':
punctuation += 1;
break;
case '`':
punctuation += 1;
break;
case '-':
punctuation += 1;
break;
case '!':
punctuation += 1;
break;
case '_':
punctuation += 1;
break;
case '(':
punctuation += 1;
break;
case ')':
punctuation += 1;
break;
case '.':
punctuation += 1;
break;
case '?':
punctuation += 1;
break;
case '"':
punctuation += 1;
break;
case ';':
punctuation += 1;
break;
}
}
words += 1;
}
System.out.println("The number of words in the file name: " + words);
System.out.println("The number of lines in the file name: " + lines);
System.out.println("The number of alphanumeric characters " + "in the file name: " + alphaNumeric);
System.out.println("The number of sentences in the file name: " + sentences);
System.out.println("The number of vowels in the file name: " + vowels);
System.out.println("The number of punctuations in the file name: " + punctuation);