Click to See Complete Forum and Search --> : help with compareTo method please


Rabbit3cat
11-09-2005, 06:01 PM
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

JjavaJ
11-09-2005, 11:49 PM
you basically need a a loop nested in another one. As you go through an array of the orignal words you add the ones that have not already occured (you use hte lops to check for this).

Oak
11-10-2005, 04:19 AM
Table1: the the the the
Table2: the the big not

... Wouldn't the common words for "the" from the above scenario be 2 rather than 6?

I think you will have to be more clear as to what you are trying to achieve.

Rabbit3cat
11-10-2005, 11:16 AM
Yes, it returns "the" two times not one time in that example, sorry my mistake.
Im not quite sure what you mean when you say
"As you go through an array of the orignal words you add the ones that have not already occured" JjavaJ.
Do you mean to create two new arrays store the words in the hash table into these arrays and then compare the arrays, and to do that would i be right in storing the elements of the hash table into the array if the format:

for(int i = 0; i < table1.hasNext(); i++){
trackArray = array[i];

//loop to read the second table into array2 here.....
//then do the same for the next loop and to compare them use

if(array[i].equals(array2[j])) {
commonWords++;

Sorry im not very good with java, is this what you mean?
Thanks