Advertisement
desislava_topuzakova

Untitled

Jul 8th, 2022
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class CountCharsInText_01 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String text = scanner.nextLine();
  9. Map<Character, Integer> symbolsCount = new LinkedHashMap<>();
  10.  
  11. for (char symbol : text.toCharArray()) {
  12. if (symbol == ' ') {
  13. continue;
  14. }
  15. //ако не съм срещала до сега символа
  16. if (!symbolsCount.containsKey(symbol)) {
  17. symbolsCount.put(symbol, 1);
  18. }
  19. //ако съм срещала до сега символа
  20. else {
  21. int currentCount = symbolsCount.get(symbol);
  22. symbolsCount.put(symbol, currentCount + 1);
  23. }
  24. }
  25.  
  26. //запис: символ -> бр. срещания
  27. symbolsCount.
  28. forEach((key, value) -> System.out.println(key + " -> " + value));
  29. }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement