hashtable
I am supposed to read input from a text file and output the frequency some of the words that will be stored in the text file:
e.g.
input:
this is a good day and it is 28Mar 2002 and i have a test today
#
THIS
IS
the line before # is the sentence and after # is the words i supposed to find the frequency
I CANNOT use java.util.Hashtable and String.hashCode ()
I have to implement own hashtable and function....
the code below is for HashMap hashmap = new HashMap ();
I need to change to HashTable hashtable = new hashtable ();
but how to change? There will be some parts to modify too....
Anyone who can help thanx...
[code]
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class freq
{
private static HashMap wordMap = new HashMap();
private static void buildWordMap(String _filename) throws IOException
{
// go to second part of text file
FileReader fr = new FileReader("freq.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while( (line = br.readLine()) != null ) {
if( line.equals("#") )
break;
}
// initilize wordMap
if( line.equals("#") ) {
while( (line = br.readLine()) != null )
wordMap.put( line.toUpperCase(), new Integer(0) ); // no occurences yet
}
else {
System.err.println("no words to seach for");
}
br.close();
}
private static void scanInput(String _filename) throws IOException
{
// for each word in first part, look for a match in wordMap
FileReader fr = new FileReader("freq.txt");
BufferedReader br = new BufferedReader(fr);
String line;
line = br.readLine();
while( (line != null) && (!line.equals("#")) ) {
StringTokenizer st = new StringTokenizer( line );
while( st.hasMoreElements() ) {
String tok = st.nextToken();
if( wordMap.containsKey(tok.toUpperCase()) ) {
Integer wordCount = (Integer)wordMap.get(tok.toUpperCase());
wordMap.put(tok.toUpperCase(), new Integer(wordCount.intValue()+1));
}
}
line = br.readLine();
}
br.close();
if( line == null )
throw new IOException("parsing error; can't find second part");
}
private static void displayResults()
{
System.out.println("RESULTS:");
Set set = wordMap.entrySet();
Iterator iterator = set.iterator();
while( iterator.hasNext() ) {
Map.Entry entry = (Map.Entry)iterator.next();
System.out.println(entry.getKey() + " occurred " +
entry.getValue() + " times.");
}
}
public static void main(String[] args) throws IOException
{
buildWordMap("freq.txt");
scanInput("freq.txt");
displayResults();
}
}

