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.

try
        {
          FileReader reader = new FileReader("mlkdream1.txt");
          Scanner in = new Scanner(reader);
          while (in.hasNext())
          {
            tmp = in.next();
            tmp = tmp.toUpperCase();
            word = new WordStat(tmp, 1);

            if(!Bst1.inTree(word))
            {
              Bst1.insert(word);
              Avl1.insert(word);
              count++;
            }
            else
            {
              Avl1.retrieve(word);
              word.updateCount(1);
              Bst1.insert(word);
              Avl1.insert(word);
            }
          }
          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 ;)

Well when I compare two Strings in java I use this, could that possibly be the problem?

String one = "iamthwee";
String two = "nonsense";

if (one.equals(two))
{
   System.out.println("Both strings are the same");
}

Whereas I can't explicitly write:

if (one == two)
{
    System.out.println("Both strings are the same");
}

Well, I was able to find out what was wrong. I was trying to do...

WordStat update;

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.

i need the program for word frequency by word length

Start a new thread and don't expect someone to give you the code unless you show some effort.
Also explain better you problem.

Edited 6 Years Ago by javaAddict: n/a

This question has already been answered. Start a new discussion instead.