-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterCounterWithFileInput.java
More file actions
39 lines (30 loc) · 1.78 KB
/
CharacterCounterWithFileInput.java
File metadata and controls
39 lines (30 loc) · 1.78 KB
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
33
34
35
36
37
38
39
import java.io.*;
public class CharacterCounterWithFileInput{
public static void main(String[] args) throws IOException {
int consonantsNum = 0, vowelsNum = 0, punctuationsNum = 0, spacesNum = 0;
File myFile = new File("C:\\Users\\ACER\\Comprog12\\Diether_Catan_Gwapa_Kaayo.txt");
FileInputStream fis = new FileInputStream(myFile);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line = br.readLine().toLowerCase();
for(int num = 0 ; num < line.length() ; num++){
switch(line.charAt(num)){
case ' ': spacesNum++; break;
case 'a': case 'e': case 'i': case 'o': case 'u': vowelsNum++; break;
case 'b': case 'c': case 'd': case 'f': case 'g': case 'h': case 'j': case 'k': case 'l': case 'm': case 'n':
case 'p': case 'q': case 'r': case 's': case 't': case 'v': case 'w': case 'x': case 'y': case 'z':
consonantsNum+=1; break;
default: punctuationsNum++; break;
}
}
System.out.println("\n Number of consonants: " + consonantsNum);
System.out.println(" Number of vowels: " + vowelsNum);
System.out.println("Number of Punctuation Characters: " + punctuationsNum);
System.out.println(" Number of spaces: " + spacesNum);
System.out.println("\n\tPERCENTAGES");
System.out.println("Consonants: " + (consonantsNum*100)/line.length() + "%");
System.out.println(" Vowels: " + (vowelsNum*100)/line.length() + "%");
System.out.println("Characters: " + (punctuationsNum*100)/line.length() + "%");
System.out.println(" Spaces: " + (spacesNum*100)/line.length() + "%");
}
}