-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCharCountIf.java
executable file
·38 lines (36 loc) · 1.37 KB
/
CharCountIf.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
import java.util.Scanner;
/**
* This program performs the same task as CharCountSwitch.java but without
* a switch statement.
*/
public class CharCountIf {
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) {
if (s.charAt(i) == '0'
|| s.charAt(i) == '1'
|| s.charAt(i) == '2'
|| s.charAt(i) == '3'
|| s.charAt(i) == '4'
|| s.charAt(i) == '5'
|| s.charAt(i) == '6'
|| s.charAt(i) == '7'
|| s.charAt(i) == '8'
|| s.charAt(i) == '9') {
digitCount++;
} else if (s.charAt(i) == '!'
|| s.charAt(i) == '?'
|| s.charAt(i) == '.') {
punctuationCount++;
} else {
letterCount++;
}
// 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);
}
}