Advertisement
desislava_topuzakova

04. Count Symbols

Jan 18th, 2022
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package Test;
  2.  
  3. import java.util.*;
  4.  
  5. public class Test {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String text = scanner.nextLine();
  9.         TreeMap <Character, Integer> symbolsCount = new TreeMap<>();
  10.         //символ -> бр. срещанията
  11.  
  12.         for (int index = 0; index < text.length(); index++) {
  13.             char currentSymbol = text.charAt(index);
  14.             //символът вече да съм го срещала
  15.             if (symbolsCount.containsKey(currentSymbol)) {
  16.                 int currentCount = symbolsCount.get(currentSymbol);
  17.                 symbolsCount.put(currentSymbol, currentCount + 1);
  18.             }//символът да не съм го срещала
  19.             else {
  20.                 symbolsCount.put(currentSymbol, 1);
  21.             }
  22.         }
  23.         //символ: бр.срещания time/s
  24.         symbolsCount.entrySet().forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue() + " time/s"));
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement