Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2e7577

Browse files
author
jsquared21
committed
Add Ex 21.4
1 parent0d6f6f2 commitb2e7577

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
2.19 KB
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*********************************************************************************
2+
* (Count consonants and vowels) Write a program that prompts the user to enter a *
3+
* text file name and displays the number of vowels and consonants in the file. *
4+
* Use a set to store the vowels A, E, I, O, and U. *
5+
*********************************************************************************/
6+
importjava.util.*;
7+
importjava.io.*;
8+
9+
publicclassExercise_21_04 {
10+
publicstaticvoidmain(String[]args)throwsException {
11+
// Create a Scanner
12+
Scannerinput =newScanner(System.in);
13+
14+
// Prompt the user to enter a text file name
15+
System.out.print("Enter a text file name: ");
16+
StringfileName =input.next();
17+
18+
// Check if file exists
19+
Filefile =newFile(fileName);
20+
if (!file.exists()) {
21+
System.out.println("The file " +fileName +" does not exist.");
22+
System.exit(1);
23+
}
24+
25+
// Create a set to store vowels
26+
Set<Character>set =newHashSet(Arrays.asList('A','E','I','O','U'));
27+
intvowels =0;// Counts the number of vowels
28+
intconsonants =0;// Counts the number of consonants
29+
30+
// Count the number of vowels and consonants in the file
31+
try (// Create an input file
32+
ScannerinputFile =newScanner(file);
33+
) {
34+
while (inputFile.hasNext()) {
35+
Stringline =inputFile.nextLine();
36+
for (inti =0;i <line.length();i++) {
37+
if (set.contains(Character.toUpperCase(line.charAt(i))))
38+
vowels++;
39+
elseif (Character.isLetter(line.charAt(i)))
40+
consonants++;
41+
}
42+
}
43+
}
44+
45+
// Display the number of vowels and consonants in the file
46+
System.out.println("The file " +fileName +" has " +vowels +
47+
" vowels and " +consonants +" consonants.");
48+
}
49+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Four score and seven years ago our fathers brought forth on this continent, a new nation,
2+
conceived in Liberty, and dedicated to the proposition that all men are created equal.
3+
4+
Now we are engaged in a great civil war, testing whether that nation, or any nation so
5+
conceived and dedicated, can long endure. We are met on a great battle-field of that war.
6+
We have come to dedicate a portion of that field, as a final resting place for those who here gave their
7+
lives that that nation might live. It is altogether fitting and proper that we should do this.
8+
9+
But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground.
10+
The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.
11+
The world will little note, nor long remember what we say here, but it can never forget what they did here.
12+
It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus
13+
far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these
14+
honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that
15+
we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new
16+
birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp