Didart

Count Symbols

Jan 20th, 2023
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. package SetsAndMaps3;
  2.  
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5.  
  6. public class CountSymbols {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String text = scanner.nextLine();
  11.  
  12.         TreeMap<Character, Integer> symbolsCount = new TreeMap<>();
  13.  
  14.         for (int index = 0; index < text.length(); index++) {
  15.             char currentSymbol = text.charAt(index);
  16.            
  17.             if (symbolsCount.containsKey(currentSymbol)) {
  18.                 int currentCount = symbolsCount.get(currentSymbol);
  19.                 symbolsCount.put(currentSymbol, currentCount + 1);
  20.             }
  21.             else {
  22.                 symbolsCount.put(currentSymbol, 1);
  23.             }
  24.         }
  25.        
  26.         symbolsCount.entrySet().forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue() + " time/s"));
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment