Advertisement
BojidarDosev

Map exercise

Mar 1st, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class exercises_Map {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6.  
  7. String input = scan.nextLine();
  8. Map<Character, Integer> counter = new LinkedHashMap<>();
  9.  
  10. for(char currChar : input.toCharArray()){
  11. if(currChar != ' ') {
  12. if (!counter.containsKey(currChar)) {
  13. counter.put(currChar, 1);
  14. } else {
  15. int br = counter.get(currChar);
  16. counter.put(currChar, br + 1);
  17. }
  18. }
  19. }
  20.  
  21. for(Map.Entry<Character, Integer> entry: counter.entrySet()){
  22. System.out.printf("%c -> %d%n", entry.getKey(), entry.getValue());
  23. }
  24. }
  25. }
  26.  
  27. /*
  28. Write a program that counts all characters in a string except space (' ').
  29.  
  30. Print all occurrences in the following format:
  31.  
  32. "{char} -> {occurrences}"
  33. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement