Word Frequency Count

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2006
Posts: 6
Reputation: Tigerdude is an unknown quantity at this point 
Solved Threads: 0
Tigerdude's Avatar
Tigerdude Tigerdude is offline Offline
Newbie Poster

Word Frequency Count

 
0
  #1
Apr 27th, 2006
I'm supposed to be inserting words from a text file into a BST and AVL tree. I've got the words to insert correctly, and it recognizes whenever the words are repeated. It just leaves the count at 2 instead of incrementing afterwards. Can't seem to figure out why it's not taking the count at the moment it finds a repeat.

  1. try
  2. {
  3. FileReader reader = new FileReader("mlkdream1.txt");
  4. Scanner in = new Scanner(reader);
  5. while (in.hasNext())
  6. {
  7. tmp = in.next();
  8. tmp = tmp.toUpperCase();
  9. word = new WordStat(tmp, 1);
  10.  
  11. if(!Bst1.inTree(word))
  12. {
  13. Bst1.insert(word);
  14. Avl1.insert(word);
  15. count++;
  16. }
  17. else
  18. {
  19. Avl1.retrieve(word);
  20. word.updateCount(1);
  21. Bst1.insert(word);
  22. Avl1.insert(word);
  23. }
  24. }
  25. reader.close();

The variable word is of the type WordStat, which is defined as (String word, int frequency). The updateCount(Integer) method is supposed to grab the original count, and increment it by the number passed to it. I don't think the retrieve is wrong, because that was already written for us.

Any help would be much appreciated
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Word Frequency Count

 
0
  #2
Apr 27th, 2006
Well when I compare two Strings in java I use this, could that possibly be the problem?
  1. String one = "iamthwee";
  2. String two = "nonsense";
  3.  
  4. if (one.equals(two))
  5. {
  6. System.out.println("Both strings are the same");
  7. }

Whereas I can't explicitly write:
  1. if (one == two)
  2. {
  3. System.out.println("Both strings are the same");
  4. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 6
Reputation: Tigerdude is an unknown quantity at this point 
Solved Threads: 0
Tigerdude's Avatar
Tigerdude Tigerdude is offline Offline
Newbie Poster

Re: Word Frequency Count

 
0
  #3
Apr 28th, 2006
Well, I was able to find out what was wrong. I was trying to do...

  1. WordStat update;
  2.  
  3. update = Avl1.retrieve(word)

I had a problem in my declarations. It would give me a comparable error everytime. I had forgotten to call Bstree<WordStat>, so that's where I messed up. It compared them correctly, that's what the inTree() function did for me. Anyways, seems there was no point to this post :rolleyes: Thanks for trying to help though.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC