fork(5) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Map;
  9. import java.util.TreeMap;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. String s = "Vê se tira notas boas!";
  17. Map<String, Integer> map = contar(s.replace(" ", "").toLowerCase());
  18. Imprimir(map);
  19. }
  20. public static Map<String, Integer> contar(String frase)
  21. {
  22. Map<String, Integer> map = new TreeMap<>();
  23. for(char item : frase.toCharArray())
  24. {
  25. Integer s = map.get(String.valueOf(item));
  26. if (s != null)
  27. {
  28. map.put(String.valueOf(item), ++s);
  29. }
  30. else
  31. {
  32. map.put(String.valueOf(item), 1);
  33. }
  34. }
  35. return map;
  36. }
  37.  
  38. public static void Imprimir(Map<String, Integer> items)
  39. {
  40. for(Map.Entry<String, Integer> a : items.entrySet())
  41. {
  42. System.out.println(a.getKey() + ": " + a.getValue());
  43. }
  44. }
  45. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
!: 1
a: 3
b: 1
e: 1
i: 1
n: 1
o: 2
r: 1
s: 3
t: 2
v: 1
ê: 1