Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Using lists in java


  • Please log in to reply

#1
bainne

bainne

    Member

  • Member
  • PipPip
  • 11 posts
Hi
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
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
it looks like common words are being counted more than once.

try adding a break after incrementing commonWord:

...
commonWord++:
break;
...

  • 0

#3
bainne

bainne

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Thanks a mill, that seems to do the trick
  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
you're welcome
  • 0

#5
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
@bainne
Rather than usuing the compareTo(String) method to check for lexicographic equivalence, the String class offers an equals(String) method that will do this. The compareTo(String) returns the difference in the ASCII value between the first two chars that are not equal; it is really only useful for alphabetization (if it returns something less than 0 or greater than 0).

P.S. The String class also provides compareToIgnoreCase(String) and equalsIgnoreCase(String). Each do exactly what it sounds like they do; they compare strings, disregarding of their case.

Hope this helps!
  • 0

#6
bainne

bainne

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Thanks Destin, that makes a good improvement. The equalsIIgnoreCase(String) method is much better suited to what i am doing than compareTo(String) i wasnt aware of it, thanks a lot!

Edited by bainne, 04 February 2006 - 05:55 AM.

  • 0

#7
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Were you just trying to count similar words between two ArrayLists? There's an easier way to do this. Using the ArrayList class's contains(Object) method, you can eliminate the use of String class's equals(String) method. Instead, you can iterate over one ArrayList, then check if the other ArrayList contains that String. This will not ignore case though. What you could do to make sure it ignores case, is when adding to the ArrayList, you can add the lower case version of that String (using the toLowerCase() method).
ArrayList<String> file1List = new ArrayList<String>();
ArrayList<String> file2List = new ArrayList<String>();

// ...

Iterator<String> it = file1List.iterator();

int commonCount = 0;
while (it.hasNext()) {
	if (file2List.contains(it.next().toLowerCase())) {
		commonCount++;
	}
}

Not exactly sure if this is what you were trying to do though.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP