This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I am working on a project and am wondering if anyone can give me some help. I am supposed to use a HashSet to get the unique count of tokens from a file. So far I am not getting a count. How does one iterate through a HashSet and add tokens? Here is my method that I have developed so far.
Any help and/or discussion is appreciated.
Thank you Oh Lord<br />For the white blind light<br />A city rises from the sea<br />I had a splitting headache<br />from which the future's made<br />--morrisson
Are you sure you're required to use HashSet rather than HashMap? The latter seems more appropriate to me. For each token found, make an entry in the Map with the token as the key, and the value is an Integer representing the number of times the token has been found so far. Each time the same token is found subsequently, you don't make a new Map entry - you just update the Integer in the existing entry to reflect the increased count.
"I'm not back." - Bill Harding, Twister
Joined: Oct 19, 2002
I see what you are saying about HashMap. We had to use that in another part of our project. For the class I am working on we are required to use HashSet. The reason being is we want to find the total number of unique tokens. HashSet will no store a tok if it already in the set, correct? I am not even sure that I need to use the iterator. So i understand the concept of what I need to do just not how to actually do it. I have looked at the api's. They do a good job of explaining the classes but not with any example. Thanks for responding.
Not surprised that you're not getting any tokens, seeing that you are iterating over the empty uniqTok Set in order to add stuff to the same Set. I guess you want to iterate over a new StringTokenizer(tok) I also don't see why you need this manipulation of a "size" variable (that isn't declared anywhere?) Can you use Java 1.4 regexps?Your instructor might consider this cheating though - Peter [ February 24, 2003: Message edited by: Peter den Haan ]