Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class CountCharsInString1 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String word = String.join("",scanner.nextLine().split(" "));
- Map<Character,Integer> countChar = new LinkedHashMap<>();
- for (int i = 0; i <word.length() ; i++) {
- char letter = word.charAt(i);
- countChar.putIfAbsent(word.charAt(i), 0);
- countChar.put(word.charAt(i), countChar.get(word.charAt(i)) + 1);
- }
- // ПЪРВИ НАЧИН НА ИЗХОД:
- // for (Map.Entry<Character, Integer> wor : countChar.entrySet()) {
- // System.out.println(wor.getKey()+" -> "+ wor.getValue());
- // }
- countChar.entrySet().forEach(e->{
- System.out.println(e.getKey()+" -> "+ e.getValue());
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement