Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Ex01_CountCharsInString {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String textInput = scanner.nextLine();
- Map<Character, Integer> table = new LinkedHashMap<>();
- for (char symbol : textInput.toCharArray()) {
- if (symbol == (' ')) {
- continue;
- } else if (!table.containsKey(symbol)) {
- table.put(symbol, 1);
- } else {
- int counter = table.get(symbol);
- table.put(symbol, counter + 1);
- }
- }
- for (Map.Entry<Character, Integer> entry : table.entrySet()) {
- char currSymbol = entry.getKey();
- int count = entry.getValue();
- System.out.printf("%c -> %d%n", currSymbol, count);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement