0

I am trying to read in a large block of text and store each unique word and the number of times it came up in the text. To do this I made an array list of a Word class. The Word class simply stores the word and number of times it came up. To avoid duplicates of the same word I use the .contains() function to see if I have already processed a word before, but for some reason, .contains() is not working.

class Main 
{
  public static void main(String[] args) throws FileNotFoundException 
  {
    File file = new File("poe.text");
    Scanner f = new Scanner(file);
    ArrayList<Word> words = new ArrayList<Word>();
    int total = 0;

    while(f.hasNext())
    {
      Word temp = new Word(f.next().toLowerCase().trim());
      //System.out.println("temp is "+temp.getWord());
      total++;
      if(words.contains(temp))
      {
        words.get(words.indexOf(temp)).up();
      } else
      {
        words.add(temp);
      }
    }

    for(Word w:words)
    {
      System.out.println(w.toString());
    }
  }
}

The if statement never evaluates to true and every word is added to the words ArrayList even if it is already present.

3
  • First of all you want Map<String, Integer> or Map<Word, Integer>. In your case my guess is that you have not overridden equals of Word class correctly. Commented Mar 31, 2020 at 9:04
  • Have a look into the contains docs. It will tell you that in absence of the respective custom overrides, it will use the default comparison method, which is reference. A new instance will always have a unique reference, regardless of the value of the instance. Commented Mar 31, 2020 at 9:04
  • You may also be interested to learn about HashSet. Commented Mar 31, 2020 at 9:20

1 Answer 1

2

In the Javadoc for List, the contains method uses the equals() method to evaluate if two objects are the same. have you implemented equals and hashcode in your words class ?

Javadoc http://docs.oracle.com/javase/7/docs/api/java/util/List.html#contains%28java.lang.Object%29

Sign up to request clarification or add additional context in comments.

2 Comments

@Ashwani It is not any more. But it looks like a "comment for answer" - answer.
@Fildor, you're correct it should have been a comment. However, just to provide a clear picture I did it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.