I am working on a program which takes a text file as input and stores each word in a hash table along with a count of how many times the word occurs in the text. It seems to work fine until I attempt a file with more than 18 words, which results in a Concurrent Modification Exception.
Here is the code I have so far
/* WordCountLL.java template: Assignment 5
Implements hash table using buckets. Java library data structure
LinkedList used to implement buckets.
*/
import java.io.*;
import java.util.*;
public class WordCountLL
{
public static final int TABLESIZE = 587;
// Next statement causes unsafe operation warning---don't like the known
// alternatives---rws
public static LinkedList<WordItem> [] hashTable = new LinkedList [TABLESIZE];
/***************************************************************************/
// hash returns the hash value for the word stored in "word".
// The hash function is to sum the ASCII codes of the characters of the word
// MOD the hash table size.
public static int hash(String word)
{
int i;
int addr = 0;
for (i = 0; i < word.length(); i++)
addr += (int) word.charAt(i);
return addr % TABLESIZE;
}
/***************************************************************************/
public static void addWord(WordItem word, int addr)
{
ListIterator iter = hashTable[addr].listIterator();
WordItem currWord;
if(!(iter.hasNext()))
{
hashTable[addr].add(word);
word.setCount(word.getCount() + 1);
}
else
{
while(iter.hasNext())
{
currWord = (WordItem)iter.next();
if(currWord.getWord().equals(word.getWord()))
{
word.setCount(word.getCount() + 1);
break;
}
else
{
hashTable[addr].add(word);
word.setCount(word.getCount() + 1);
}
}
}
}
/*public static WordItem maxCount(LinkedList list)
{
ListIterator iter = list.listIterator();
int maxCount = 0;
WordItem currWord = new WordItem();
while(iter.hasNext())
{
currWord = (WordItem)iter.next();
if(currWord.getCount() > maxCount)
{
maxCount = currWord.getCount();
}
}
return currWord;
}*/
//public static
public static void main(String[] args)
{
//***** Hash table initialization goes here
for(int i = 0; i < TABLESIZE; i++)
{
hashTable[i] = new LinkedList();
}
if (args.length < 2)
{
System.out.print("ERROR: insufficient number of command line ");
System.out.println("arguments. Program aborted.");
return;
}
int numWords = Integer.parseInt(args[1]);
// Call book iterator with name of file (specified in args[0])
BookIterator bookIterator = new BookIterator();
bookIterator.readBook(args[0]);
// Go through iterator storing words into hashtable
while(bookIterator.hasNext())
{
WordItem word = new WordItem(bookIterator.next());
addWord(word, hash(word.getWord()));
}
// Code for producing output goes here
int totalWords = 0;
for(int i = 0; i < TABLESIZE; i++)
{
totalWords = hashTable[i].size() + totalWords;
}
System.out.println("There were a total of " + totalWords + " unique words");
}
}