TreeSet goodWords = new TreeSet();
...
Iterator iterGoodWords = goodWords.iterator();

while (iterGoodWords.hasnext()) {
    String word = iterGoodWords.next();
    System.out.println(word);
}

Tell me, why doesn’t that print anything even if there are some items in the goodWords collection? I know there are items in the set because when I check the size, it’s not zero. It seems that iterGoodWords.hasnext() always evaluates to false.

Javac compiles it. It just doesn’t print.

Edited 5 Years Ago by Skyelher: n/a

OK, we don't have the full picture here.
You declare goodWords as a raw type - which gives a warning when compiled. Ditto your iterator.
Line 5 has a case error, and doesn't compile.
You then assign the return value of iterGoodWords.next(), which is type Object, to a String, which generates a compile error.
So exactly which "javac" compiles it?

Edited 5 Years Ago by JamesCherrill: n/a

I use the -Xlint:unchecked against the warning. It's how get through the warning but it never pointed to that. It doesn't look like that in my file. Sorry about the case error. My fault. It's not the same in my actual .java. This is what I was trying to do:

Scanner words;
        HashSet<String> dict = new HashSet<String>();
        Scanner userFile;
        
        try {
        
            words = new Scanner(new File("words.txt"));
        
            while (words.hasNext()) {
                String word = words.next();
                dict.add(word.toLowerCase());
            }
            
            userFile = new Scanner(getInputFileNameFromUser());
            
            // Skip over any non-letter characters in the file.
            userFile.useDelimiter("[^a-zA-Z]+");
            
            HashSet<String> badWords = new HashSet<String>();
            while (userFile.hasNext()) {
                String userWord = userFile.next();
                userWord = userWord.toLowerCase();
                if (!dict.contains(userWord) && 
                    !badWords.contains(userWord)) {
                    
                    badWords.add(userWord);
                    TreeSet<String> goodWords = new TreeSet<String>();
                    goodWords = corrections(userWord, dict);
                    System.out.print(userWord + ": ");
                    if (goodWords.isEmpty())
                        System.out.println("(no suggestions)");
                    else {
                        int count = 0;
                        for (String goodWord: goodWords) {
                            System.out.print(goodWord);
                            if (count < goodWords.size() - 1)
                                System.out.print(", ");
                            else
                                System.out.print("\n");
                            count++;
                        }
                    }
                    
                }
            
            }
            
        }
        catch (FileNotFoundException e) {
            System.exit(0);
        }

I couldn't make the iterator work so I went ahead and used for-each loops. It worked wonders for me. But I wanted to learn how to use iterators and I couldn't find the fault. When I use Eclipse to step one line at a time iterGoodWords.hasNext() seems to evaluate to false as it wouldn't go inside the loop.

By the way, the syntax I used in the creation of an iterator, Iterator iterGoodWords = goodWords.iterator();, was modeled from our textbook so I didn't know I had to do type cast. My fault, I guess.

Edited 5 Years Ago by Skyelher: n/a

Yes, the enhanced for/each loop is great. I still can't see why your Iterator seems to be empty. If you can cut this down to a small stand-alone class that demonstrates the problem I'll try to run and diagnose it. Please, when you do find out, please post the answer here so we call all learn something.

If you have HashSet<String> then the iterator will be similarly be Iterator<String>, which makes your code simpler and/or safer.

Do you have any special character in your goodWord list? I mean any character which may not be a displayable character? Just want to be sure about the TreeSet when you use raw type. :)

I'll try to do that. I was rather pissed that it wasn't working so I went ahead and didn't bother storing my old version.

Special characters? I don't think so. See, a word will only reach goodWords if it can be found in our instructor's dictionary. We're sort of building a simple spell-checker.

Though, I didn't try a parameterized iterator. I should do that.

Thanks, everyone. I've found out that my mistake was on the non-use of a parameter in my iterator. It works now as it should.

Iterator<String> iterGoodWords;
iterGoodWords = goodWords.iterator();
while (iterGoodWords.hasNext()) {
    String item = iterGoodWords.next();
    System.out.print(item);
    if (iterGoodWords.hasNext())
        System.out.print(", ");
    else
        System.out.print("\n");
}
This question has already been answered. Start a new discussion instead.