Java: WordCount mit Vector

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Java: WordCount mit Vector

    Quellcode

    1. import java.io.File;
    2. import java.io.IOException;
    3. import java.util.*;
    4. public class A3_WordCount_Vector {
    5. static Vector words = new Vector();
    6. @SuppressWarnings("unchecked")
    7. public static void main(String[] args) throws IOException
    8. {
    9. Scanner In = new Scanner(new File("wordcount.dat"));
    10. while(In.hasNext()) {
    11. words.addElement((String)In.next());
    12. }
    13. In.close();
    14. printTable();
    15. }
    16. /**
    17. * Zählt die Vorkommen des Wortes
    18. * löscht das Element aus dem Vector, nachdem es gezählt wurde
    19. * @param word
    20. * @param tmp
    21. * @return
    22. */
    23. static int countWord(String word, Vector tmp) {
    24. int n=0;
    25. while(tmp.contains(word))
    26. {
    27. tmp.remove(tmp.indexOf(word));
    28. n++;
    29. }
    30. return n;
    31. }
    32. /**
    33. * gibt die fertige Hashtable aus
    34. *
    35. */
    36. static void printTable() {
    37. Iterator it = words.iterator();
    38. Vector tmp = new Vector();
    39. tmp.addAll(words);
    40. while(it.hasNext()) {
    41. String word = (String) it.next();
    42. if(tmp.contains(word))
    43. System.out.println(word + ":\t" + countWord(word, tmp) + "mal");
    44. }
    45. }
    46. }
    Alles anzeigen