Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
110
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 CountCharsInString1 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String word = String.join("",scanner.nextLine().split(" "));
  10.  
  11.  
  12. Map<Character,Integer> countChar = new LinkedHashMap<>();
  13.  
  14. for (int i = 0; i <word.length() ; i++) {
  15. char letter = word.charAt(i);
  16.  
  17. countChar.putIfAbsent(word.charAt(i), 0);
  18. countChar.put(word.charAt(i), countChar.get(word.charAt(i)) + 1);
  19.  
  20. }
  21. // ПЪРВИ НАЧИН НА ИЗХОД:
  22. // for (Map.Entry<Character, Integer> wor : countChar.entrySet()) {
  23. // System.out.println(wor.getKey()+" -> "+ wor.getValue());
  24. // }
  25.  
  26. countChar.entrySet().forEach(e->{
  27. System.out.println(e.getKey()+" -> "+ e.getValue());
  28. });
  29.  
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement