daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest May 16th, 2018 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Map;
  2. import java.util.Scanner;
  3. import java.util.TreeMap;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         Scanner stringScan = new Scanner(System.in);
  9.  
  10.         int howManyRows = scanner.nextInt();
  11.         String text;
  12.  
  13.         Map<Character, Integer> characters = new TreeMap<>();
  14.  
  15.         for(int i=0; i<howManyRows; i++){
  16.             text = stringScan.nextLine();
  17.             countCharactersInString(text, characters);
  18.         }
  19.  
  20.         System.out.println(buildAnswer(characters));
  21.     }
  22.  
  23.     private static void countCharactersInString(String text, Map<Character, Integer> characters){
  24.         for(char c : text.toCharArray()){
  25.             if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
  26.                 if(characters.containsKey(c)){
  27.                     characters.replace(c, characters.get(c) + 1);
  28.                 }
  29.                 else {
  30.                     characters.put(c, 1);
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36.     private static String buildAnswer(Map<Character, Integer> characters){
  37.         StringBuilder lowerCases = new StringBuilder();
  38.         StringBuilder upperCases = new StringBuilder();
  39.  
  40.        
  41.         for(Map.Entry<Character, Integer> entry : characters.entrySet()){
  42.             if(entry.getKey() >= 'a' && entry.getKey() <= 'z'){
  43.                 lowerCases.append(entry.getKey());
  44.                 lowerCases.append(" ");
  45.                 lowerCases.append(entry.getValue());
  46.                 lowerCases.append("\n");
  47.             }
  48.             else {
  49.                 upperCases.append(entry.getKey());
  50.                 upperCases.append(" ");
  51.                 upperCases.append(entry.getValue());
  52.                 upperCases.append("\n");
  53.             }
  54.         }
  55.  
  56.         return lowerCases.append(upperCases).toString();
  57.     }
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top