-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCharCountSwitch.java
executable file
·41 lines (39 loc) · 1.29 KB
/
CharCountSwitch.java
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
import java.util.Scanner;
public class CharCountSwitch {
public static void main(String[] args) {
System.out.print("Enter a string of characters: ");
String s = new Scanner(System.in).nextLine();
int digitCount = 0, punctuationCount = 0, letterCount = 0;
for (int i = 0; i < s.length(); ++i) {
switch (s.charAt(i)) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
// Fall-through matches all digits
digitCount++;
break;
case '!':
case '?':
case '.':
// Fall-through matches all punctuation
punctuationCount++;
break;
default:
// Others are assumed to be letters
letterCount++;
// break is optional after the default case
}
// Will the code above provide an accurate count of letters?
}
System.out.printf("Your input contained %d digits, %d "
+ "punctuaion marks, and %d letters.%n",
digitCount, punctuationCount, letterCount);
}
}