Advertisement
IvaAnd

Ex01_CountCharsInString

Jul 14th, 2020
83
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.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class Ex01_CountCharsInString {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String textInput = scanner.nextLine();
  10. Map<Character, Integer> table = new LinkedHashMap<>();
  11.  
  12. for (char symbol : textInput.toCharArray()) {
  13. if (symbol == (' ')) {
  14. continue;
  15. } else if (!table.containsKey(symbol)) {
  16. table.put(symbol, 1);
  17. } else {
  18. int counter = table.get(symbol);
  19. table.put(symbol, counter + 1);
  20. }
  21. }
  22.  
  23. for (Map.Entry<Character, Integer> entry : table.entrySet()) {
  24.  
  25. char currSymbol = entry.getKey();
  26. int count = entry.getValue();
  27.  
  28. System.out.printf("%c -> %d%n", currSymbol, count);
  29.  
  30. }
  31.  
  32. }
  33.  
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement