Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class exercises_Map {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String input = scan.nextLine();
- Map<Character, Integer> counter = new LinkedHashMap<>();
- for(char currChar : input.toCharArray()){
- if(currChar != ' ') {
- if (!counter.containsKey(currChar)) {
- counter.put(currChar, 1);
- } else {
- int br = counter.get(currChar);
- counter.put(currChar, br + 1);
- }
- }
- }
- for(Map.Entry<Character, Integer> entry: counter.entrySet()){
- System.out.printf("%c -> %d%n", entry.getKey(), entry.getValue());
- }
- }
- }
- /*
- Write a program that counts all characters in a string except space (' ').
- Print all occurrences in the following format:
- "{char} -> {occurrences}"
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement