Advertisement

07.22.2008 at 08:00AM PDT, ID: 23585343
[x]
Attachment Details

Using hashtables to count the number of occurence of a word

Asked by Atouray in Sun Java System Web Server, Java Programming Language, Java Application Servers

Tags: Java, java 5.0

I am using hashtable to count the word frequency of any all the words in my text file. eg if the word "good" appears 10 times in my text file it should print "good 10" my code can only read the first line of the text file and cannot cannot continue further, i have been trying to figure it out but its still a problem. I have attached the code so far.Start Free Trial
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:
import java.io.*;
import java.util.*;
 
public class WordFrequency {
	public static void main(String[] args) throws IOException {
		WordFrequency test = new WordFrequency();
		test.countWordFre();
 
	}
 
	public void countWordFre() throws IOException {
		Scanner in = new Scanner(new FileInputStream("in.txt"));
		PrintWriter out = new PrintWriter(new FileOutputStream("out.txt"));
		Hashtable map = new Hashtable();
		String line = in.nextLine();
		String word;
		StringTokenizer st = new StringTokenizer(line);
		while (st.hasMoreTokens()) {
			word = st.nextToken().toLowerCase();
			if (map.containsKey(word)) {
				int count = (Integer) map.get(word);
				map.put(word, count + 1);
			} else {
				map.put(word, 1);
			}
 
		}
       word=in.nextLine();
		Enumeration<String> e = map.keys();
		while (e.hasMoreElements()) {
			 word =e.nextElement();
			out.println(word + " " + map.get(word));
		}
 
		out.println(map.size());
		out.println(map.isEmpty());
		out.close();
	}
}
[+][-]07.23.2008 at 01:35AM PDT, ID: 22067095

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.23.2008 at 05:54AM PDT, ID: 22068683

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]07.23.2008 at 06:06AM PDT, ID: 22068814

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Sun Java System Web Server, Java Programming Language, Java Application Servers
Tags: Java, java 5.0
Sign Up Now!
Solution Provided By: Bart_Cr
Participating Experts: 3
Solution Grade: A
 
 
[+][-]07.23.2008 at 06:07AM PDT, ID: 22068825

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]07.23.2008 at 06:11AM PDT, ID: 22068881

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628