You are not logged in.

  • Login

1

Thursday, March 23rd 2006, 5:48pm

Java: WordCount mit Vector

Java Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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");
		}
	}
 
}

Similar threads

Social bookmarks