Java: I've created a list of word objects to include the name and the frequency, but having trouble

Posted by adam08 on Stack Overflow See other posts from Stack Overflow or by adam08
Published on 2010-05-01T22:01:42Z Indexed on 2010/05/01 22:07 UTC
Read the original article Hit count: 167

Filed under:
|

Hi Everyone,

I'm working on a project which has a dictionary of words and I'm extracting them and adding them to an ArrayList as word objects. I have a class called Word as below.

What I'm wondering is how do I access these word objects to update the frequency? As part of this project, I need to only have one unique word, and increase the frequency of that word by the number of occurrences in the dictionary.

Word(String word)
{
  this.word = word;
  this.freq = 0;
}

public String getWord() { 
    return word;
}

public int getFreq() {
   return freq;
}

public void setFreq() {
   freq = freq + 1;
}

This is how I am adding the word objects to the ArrayList...I think it's ok?

String pattern = "[^a-zA-Z\\s]";
        String strippedString = line.replaceAll(pattern, "");
        line = strippedString.toLowerCase();
        StringTokenizer st = new StringTokenizer(line);
        while (st.hasMoreTokens())
        {
            String newWord = st.nextToken();
            word.add(new Word(newWord));
            count++;
        }

© Stack Overflow or respective owner

Related posts about java

Related posts about objects