tags:

views:

55

answers:

3

I am not trying to duplicate threads here. My problem is i am piping in a file using msdos called amazon.txt the file has 637 words in it.. I want a count of unique words.. and not a count of "a", "the" , "this" which i havent counted for yet in the code..

when i add to a tree set it only has 8 words.. There should be atlest 300 unique words..

count of total file = 637 count2 of treeset = 8

I thought treeset handles duplicates? what am i doing wrong? The file does contain some ints an $

import java.util.Scanner;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.HashSet;

public class practice1
{
    public static void main(String[] args)
    {
        Scanner     sc = new Scanner(System.in);    
        String      word;
        //String grab;
        int count = 0;
        int count2 =0;
        int count3 =0;
        int count4 =0;
        int number;
        //ArrayList<String> a = new ArrayList<String>();
        TreeSet<String> a = new TreeSet<String>();

        while (sc.hasNext()) 
        {
            word = sc.next();
            count++;           // 637 words
            a.add(word);
            if (word.equals("---"))
            {
                break;
            }
        }

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

        while(it.hasNext())
        {
            string grab = it.next(); 
            count2++;                   // 8 words

            if (grab.equals("---"))
            {
                break;
            }
        }

        System.out.println("count2");
        System.out.println(count2);
        System.out.println("count");
        System.out.println(count);          
        System.out.println("\nbye...");
    }
}
A: 

I don't see anywhere where you are adding the word into the TreeSet 'a'.

If I'm just missing that (and I might be) I'd bet the problem is that a TreeSet is not guaranteed to iterate in the order of insertion. That is, you add "---" last but there's no reason it won't come out of the iterator 8th and terminate your program.

So I'd say get rid of the check where you see if the iterator returns "---" and see where that gets you.


Had time to verify, change:

    if (grab.equals("---"))
    {
        break;
    }

to:

    if (grab.equals("---"))
    {
        //break;
    }

and it works as expected.

Good luck!

anq
I was commenting out some code and i accidently took out the add part. i added it back in..
icelated
+2  A: 

Your method for counting the number of entries in the TreeSet is to iterate over the Set and stop counting when you first see the string "---".

This isn't correct. You are probably assuming that the order of entries returned by TreeSet.iterator() is the same order as which they were inserted in. That isn't the case:

The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

"Natural ordering" here means the results of String.compareTo(String) (since String implements Comparable<String>), which tests for lexicographical order. In other words, a the iterator of a TreeSet<String> returns the items in alphabetical order.

If you want to know the size of your Set, just use size().

matt b
good catch... I'd just not add the --- and it should probably work (I didn't add it for my testing... oops). Just move the if (word.equals("---")) part above the add.
TofuBeer
No, you should change the way in which you are counting the size of the TreeSet. You can't just stop when you find a token, because the order of iteration is not the same as the order of insertion.
matt b
A: 

There is no need to iterate a 2nd time, just replace 2nd loop with

System.out.println("Treeset.size():" + a.size() );

and do not add "---" to treeset in the first loop (assuming this is some kind of end of file marker)

if (word.equals("---"))
{
    break;
}
a.add(word);
stacker