Repository navigation
Expand file tree
/
Copy pathCharactersCount.java
More file actions
90 lines (77 loc) · 1.58 KB
/
Copy pathCharactersCount.java
File metadata and controls
90 lines (77 loc) · 1.58 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.Scanner;
public class CharactersCount {
public static void main(String[] args) {
// User inputs a message, program returns information about characters in
// message
Scanner sc = new Scanner(System.in);
System.out.println("Enter a message:");
String entry = sc.nextLine();
sc.close();
// for loop
System.out.println("Message: " + entry);
int length = entry.length();
System.out.println("Total Characters: " + length);
entry = entry.toLowerCase();
int spaces = 0;
int vowels = 0;
int consonants = 0;
int numbers = 0;
int others = 0;
for (int i = 0; i < length; i++) {
switch (entry.charAt(i)) {
case ' ':
spaces++;
break;
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
vowels++;
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 'z':
consonants++;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
numbers++;
break;
default:
others++;
}
}
System.out.println("Number of Spaces: " + spaces);
System.out.println("Number of Vowels: " + vowels);
System.out.println("Number of Consonants: " + consonants);
System.out.println("Number of Numbers: " + numbers);
System.out.println("Number of Other Characters: " + others);
}
}