0

I have two arraylist fileWordList and stopWordList. I want to compare fileWordList to stopWordList and delete any same words from fileWordList. My codes are as below:

ArrayList<String> fileWordList = new ArrayList<String>();
ArrayList<String> stopWordList = new ArrayList<String>();

for (Iterator<String> i = fileWordList.iterator(); i.hasNext(); ) {

    for(Iterator<String> j = stopWordList.iterator(); j.hasNext();){
        if (j.next() == i.next()){
           i.remove();
        }
    }

}

The error was caused by: java.util.NoSuchElementException. How to solve this? What is wrong with the code?

2
  • What do you mean by "similar"? Can you give an example of words that you consider to be similar, but which would not compare equal? Commented Jul 29, 2012 at 15:59
  • Consider using Set instead of List for large collections. Then you could do: fileWordSet.removeAll(stopWordSet). This would also take care of multiple occurences. Commented Jul 29, 2012 at 16:46

3 Answers 3

4

You are calling i.next() too many times. You should call it once per iteration of the outer loop and remember the result.

for (Iterator<String> i = fileWordList.iterator(); i.hasNext(); ) {
    String s = i.next();
    for(Iterator<String> j = stopWordList.iterator(); j.hasNext();){
        if (j.next() == s){ // consider using equals here
           i.remove();
           break; // No need to continue checking.
        }
    }

}

I suspect that you want to use j.next().equals(s) to compare two strings for equality.

You may also want to consider using ArrayList.contains. Or use a HashSet to store your stop words, as you probably don't care about the order in which they are stored.

Sign up to request clarification or add additional context in comments.

3 Comments

You are better off using the for-each syntax.
if ArrayList.contains is used, does it means I don't have to iterate the list? and how do I know which element to remove?
Using ArrayList.contains(Object o) means you don't have to iterate it yourself. When you call i.remove it will remove the the element i.next() returned. That's why you have an if statement to check if it is in the list. Also as @Havard suggested, it's much simpler to use a for each loop`.
1

Why not use the ArrayList contains() method instead of nested loop?

for (Iterator<String> i = fileWordList.iterator(); i.hasNext(); ) {

    String word = i.next();

    // Only removes first occurence.
    if (stopWordList.contains(word)) {
        i.remove();
    }
}

// Better and simpler solution
for (String word : stopWordList) {
    while(fileWordList.contains(word)) {
        fileWordList.remove(word);
    }
}

3 Comments

this one is good and precise but it doesn't consider words starting with uppercase. is there a way for that?
The string class has a special method for that. String#equalsIgnoreCase(String anotherString)
@mellissa Edit: Actually, that didn't seem to work. Havard's suggestion should work.
0
for(String word : stopWordList) 
    if(fileWordList.contains(word)) 
        fileWordList.remove(word)

1 Comment

hm, i didn't cover multiple occurances of word in the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.