compareTo() method


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: compareTo() method

  1. #1
    Join Date
    Oct 2005
    Posts
    16

    compareTo() method

    Hi
    I am trying to compare words in strings, the strings are stored in hash tables. The first file is stored in testTable and the second in TrainingTable. What i want to do is increment the integer variable "commonWords" by one each time, a word in testTable matches a word in TrainingTable, i have been using the following code to do this, and it does compare the two strings but it does not include duplicates. So if the testTable contains the word "the" four times and trainingTable contains the word "the" two times i want commonwords to return 6 but currently it only returns one. does anybody know how i can solve this problem?

    Iterator table1 = testTable.keySet().iterator();
    while (table1.hasNext()) {

    Object e = table1.next();
    Iterator table2 = trainingTable.keySet().iterator();
    while(table2.hasNext()) {
    Object f = table2.next();

    if(f.toString().compareTo(e.toString()) == 0) {
    commonWords++;
    }

    }


    }

    Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You are iterating through a "set" to view the elements of your table. The normal function of a set is that it allows only one copy of any entry. Therefore, your two tables would each have only one entry "the", no matter how many times it occurs.

    Either find another data structure which holds your "tokens" and allows you multiple entries, or find another search and count algorithm (such as KMP) which permits you to tokenize your first passage and then match it against the second ...

  3. #3
    Join Date
    Oct 2005
    Posts
    16
    Thanks, i didnt realise that a hash table only held one entry of each word. I wouldnt know how to start on implementing a KMP algorithm my programming skills are rather minimal. Do you know of any other data structure that hold duplicate values, i was considering arrays, but the size of an array must be set in advance and the size of the files to be compared will continually vary in size...

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    It isn't that hash tables only include one copy, it is when you create a keySet you include only one occurrence of each word. In your method you are iterating over the keySet returned from the hash table, so you are getting only one occurrence of each word in the file.

    There are other "provided structures", such as an ArrayList, which acts like an array and like a list, and can expand as needed. ArrayList would make your processing more direct ... because you can address it by the index of the element in the list ... or you can use an iterator to move through the structures as you are doing with your hash table ...

    Since you are comparing strings, use the String class's "equals()" (or "equalsIgnoreCase") method to test your match -

    Code:
    while e.hasNext()
    {
      counter = 0;
      string test = e.next()
      {
        while( f.hasNext()) // make sure you start at the front of f
        {
           if test.equals(f.next())
           {
             counter++;
           }
        }
        System.out.println( "The string " + test + "occurs " + counter + " times");
      }
    }
    Last edited by nspils; 11-10-2005 at 07:38 PM.

Similar Threads

  1. jdi popframes method
    By caid in forum Java
    Replies: 0
    Last Post: 09-01-2005, 01:12 AM
  2. Replies: 3
    Last Post: 03-06-2005, 07:16 PM
  3. GetRows method
    By Jason West in forum VB Classic
    Replies: 1
    Last Post: 03-28-2002, 08:11 PM
  4. depricated method needs changing
    By Alan Shiers in forum Java
    Replies: 0
    Last Post: 03-15-2002, 12:51 PM
  5. Replies: 3
    Last Post: 04-13-2001, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center