View difference between Paste ID: HdCpEQBu and aMDzjzQ4
SHOW: | | - or go back to the newest paste.
1
import java.util.*;
2
3
public class Demo {
4
    public static void main(String[] args) {
5
        Scanner scanner = new Scanner(System.in);
6
        //1. прочитам текст
7
        //2. обходим всички символи
8
        String text = scanner.nextLine();
9
        Map<Character, Integer> lettersCount = new LinkedHashMap<>();
10
        //символ -> брой на срещанията
11
        //0 до последния
12
        for (int index = 0; index <= text.length() - 1; index++) {
13
            char currentSymbol = text.charAt(index);
14
            if (currentSymbol == ' ') {
15
                continue;
16
            }
17
18
            //проверка вече имам ли такъв символ
19
            //ако не съм го срещала
20
            if (!lettersCount.containsKey(currentSymbol)) {
21
                lettersCount.put(currentSymbol, 1);
22
            }
23
            //ако съм го срещала
24
            else {
25
                int currentCount = lettersCount.get(currentSymbol); //текущия брой
26
                lettersCount.put(currentSymbol, currentCount + 1);
27
            }
28
29
        }
30
31
        //lettersCount.keySet() -> всички ключове
32
        //lettersCount.values() -> всички стойности
33
        //lettersCount.entrySet() -> всички записи (ключ -> стойност)
34
35
36
        //всички записи: {char} -> {occurrences}
37
        //foreach
38
        for (Map.Entry<Character, Integer> entry : lettersCount.entrySet()) {
39
                //{char} -> {occurrences}
40
            System.out.println(entry.getKey() + " -> " + entry.getValue());
41
        }
42
43
        //stream
44
        //lettersCount.entrySet().forEach(entry -> System.out.println(entry.getKey() + " -> " + entry.getValue()));
45
46
47
    }
48
}