IrinaIgnatova

Count Chars In A String

Jul 16th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. Scanner scanner = new Scanner(System.in);
  12.  
  13. String text = scanner.nextLine();
  14.  
  15. Map<Character, Integer> table = new LinkedHashMap<>();
  16. for (char symbol : text.toCharArray()) {
  17. if (symbol != ' ') {
  18. if (!table.containsKey(symbol)) {
  19. table.put(symbol, 1);
  20. } else {
  21. int currentCount = table.get(symbol);
  22. table.put(symbol, currentCount + 1);
  23. }
  24. }
  25. }
  26. for (Map.Entry<Character, Integer> entry : table.entrySet()) {
  27. System.out.printf("%c -> %d%n", entry.getKey(), entry.getValue());
  28. }
  29.  
  30.  
  31. }
  32.  
  33. }
Add Comment
Please, Sign In to add comment