Advertisement
Guest User

Untitled

a guest
May 25th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. package prIndicePalabras;
  2.  
  3.  
  4. import java.io.PrintWriter;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. import java.util.SortedMap;
  8. import java.util.TreeMap;
  9.  
  10. public class IndiceContador extends IndiceAbstracto {
  11. private SortedMap<String, Integer> indice;
  12.  
  13. public IndiceContador() {
  14. indice = new TreeMap<>();
  15. }
  16.  
  17. private void anyadir(String pal) {
  18. pal = pal.toLowerCase().trim();
  19. Integer cnt = indice.get(pal);
  20. if (cnt == null) {
  21. indice.put(pal,1);
  22. } else {
  23. indice.put(pal, cnt+1);
  24. }
  25. }
  26. public void resolver(String delim) {
  27. indice.clear();
  28.  
  29. for(String f:frases) {
  30. try (Scanner sc = new Scanner(f)) {
  31. sc.useDelimiter(delim);
  32. while(sc.hasNext()) {
  33. anyadir(sc.next());
  34. }
  35. }
  36. }
  37. }
  38.  
  39. public void presentarIndice(PrintWriter pw) {
  40. for (Map.Entry<String, Integer> e: indice.entrySet()) {
  41. System.out.printf("%-10s %4d\n", e.getKey(), e.getValue());
  42. }
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement