Quellcode
- import java.io.File;
- import java.io.IOException;
- import java.util.*;
- public class A3_WordCount_Vector {
- static Vector words = new Vector();
- @SuppressWarnings("unchecked")
- public static void main(String[] args) throws IOException
- {
- Scanner In = new Scanner(new File("wordcount.dat"));
- while(In.hasNext()) {
- words.addElement((String)In.next());
- }
- In.close();
- printTable();
- }
- /**
- * Zählt die Vorkommen des Wortes
- * löscht das Element aus dem Vector, nachdem es gezählt wurde
- * @param word
- * @param tmp
- * @return
- */
- static int countWord(String word, Vector tmp) {
- int n=0;
- while(tmp.contains(word))
- {
- tmp.remove(tmp.indexOf(word));
- n++;
- }
- return n;
- }
- /**
- * gibt die fertige Hashtable aus
- *
- */
- static void printTable() {
- Iterator it = words.iterator();
- Vector tmp = new Vector();
- tmp.addAll(words);
- while(it.hasNext()) {
- String word = (String) it.next();
- if(tmp.contains(word))
- System.out.println(word + ":\t" + countWord(word, tmp) + "mal");
- }
- }
- }