Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prIndicePalabras;
- import java.io.PrintWriter;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.SortedMap;
- import java.util.TreeMap;
- public class IndiceContador extends IndiceAbstracto {
- private SortedMap<String, Integer> indice;
- public IndiceContador() {
- indice = new TreeMap<>();
- }
- private void anyadir(String pal) {
- pal = pal.toLowerCase().trim();
- Integer cnt = indice.get(pal);
- if (cnt == null) {
- indice.put(pal,1);
- } else {
- indice.put(pal, cnt+1);
- }
- }
- public void resolver(String delim) {
- indice.clear();
- for(String f:frases) {
- try (Scanner sc = new Scanner(f)) {
- sc.useDelimiter(delim);
- while(sc.hasNext()) {
- anyadir(sc.next());
- }
- }
- }
- }
- public void presentarIndice(PrintWriter pw) {
- for (Map.Entry<String, Integer> e: indice.entrySet()) {
- System.out.printf("%-10s %4d\n", e.getKey(), e.getValue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement