What i am trying to do is find all common words in two text files. I have broken the two files into single words which are stored in arrayLists, one for each file. To compare the two files i am using the following:
Iterator table1 = list1.iterator();
while (table1.hasNext()) {
Object e = table1.next();
Iterator table2 = list2.iterator();
while(table2.hasNext()) {
Object f = table2.next();
if(f.toString().compareTo(e.toString()) == 0) {
commonWords++;
}
If the two files contain the same words, and they are as follows:
"a one one two three sixty five a sixty five"
Then the result in commonWords is 18, but the result should be 10.
and if the files are different such that the first is the same as above, but the second is:
"a tree which one three tall"
Then in this case the result would be 5, instead of 3
I know why the problem is occuring everytime the current word in list1 is found in list2, 'commonWords' is incremented by one, but i cannot figure out how to implement it such that it will only return, taking the first example, 10 in 'commonWords'.
If anyone can see how this problem can be solved it would be greatly appreciated!
Thanks