-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathCountingCharacters.java
More file actions
41 lines (29 loc) · 1.27 KB
/
CountingCharacters.java
File metadata and controls
41 lines (29 loc) · 1.27 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
package studio3;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
public class CountingCharacters {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string of characters: ");
String quote = input.nextLine().toLowerCase();
input.close();
// String quote = "If the product of two terms is zero then common sense says at least one of the two terms has to be zero to start with. " +
// "So if you move all the terms over to one side, you can put the quadratics into a form that can be factored allowing that side of the equation to equal zero." +
//" Once you’ve done that, it’s pretty straightforward from there.";
char [] charactersInString = quote.toCharArray();
HashMap <Character, Integer> count = new HashMap<>();
for(char i:charactersInString) {
if (count.containsKey(i)) {
count.put(i, count.get(i) + 1);
} else {
count.put(i, 1);
}
}
for (Map.Entry entry : count.entrySet()){
System.out.println(entry.getKey() + " " + entry.getValue());
}
// main(quote);
}
}