Write a program that reads some text from the console and counts the occurrences of each character in it. Print the results in alphabetical (lexicographical) order.

Examples:

 Input Output SoftUni rocks : 1 time/s (empty space) S: 1 time/s U: 1 time/s c: 1 time/s f: 1 time/s i: 1 time/s k: 1 time/s n: 1 time/s o: 2 time/s r: 1 time/s s: 1 time/s t: 1 time/s

 Input Output Did you know Math.Round rounds to the nearest even integer? : 9 time/s (empty space) .: 1 time/s ?: 1 time/s D: 1 time/s M: 1 time/s R: 1 time/s a: 2 time/s d: 3 time/s e: 7 time/s g: 1 time/s h: 2 time/s i: 2 time/s k: 1 time/s n: 6 time/s o: 5 time/s r: 3 time/s s: 2 time/s t: 5 time/s u: 3 time/s v: 1 time/s w: 1 time/s y: 1 time/s

+1 vote

Here's my solution - the key row here is row number 15 (the highlighted one):

```import java.util.*;

public class Pr_04_CountSymbols {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split("");//GETTING THE INPUT AS AN ARRAY OF STRINGS

TreeMap<String, Integer> treeMap = new TreeMap<>();//USING THE TreeMap SO THE ELEMENTS WHEN PRINTING ARE IN ALPHABETICAL (LEXICOGRAPHICAL) ORDER

for (int i = 0; i < input.length; i++) {//ITERATE THROUGH THE ARRAY input[]
if (!treeMap.containsKey(input[i])) {//IF THERE ISN'T KEY input[i]IN THE treeMap...
} else if (treeMap.containsKey(input[i])) {//IF THERE IS ALREADY KEY input[i] IN THE treeMap...
treeMap.put(input[i], (treeMap.get(input[i]) + 1));//...ADD IT THERE + ADD TO THE CURRENT VALUE OF THIS KEY 1
}
}

for (Map.Entry<String, Integer> entry : treeMap.entrySet()) {
System.out.printf("%s: %s time/s\n", entry.getKey(),entry.getValue());
}
}
}
```