• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write strings from a HashSet to an output file, each token on a separate line?

 
John Sannito
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My project requires an application that will read a text file(up to 20 mg in size), parse it for individual tokens(words), and output a summary report of the total tokens processed, as well as write all the unique tokens to an output file, each token on a separate line. In it's final form it will have five classes per the requirements, but my instructor requires that we first write test code that works. I have written one class as test code that reads the file, splits the strings where there is white space and puts them in a HashSet. I can iterate the HashSet and display each unique token to the screen on it's own line, but when I write the HashSet to an output file ( in my try finally statement) it writes the file with the tokens separated by a comma. I tried adding system.out.println() below the System.out.println(hashSetIterator.next()), but that won't even compile. I'm very much a novice. Just writing what I have here took me about 7 hours, so please be gentle!


 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sannito wrote:I can iterate the HashSet and display each unique token to the screen on it's own line, but when I write the HashSet to an output file ( in my try finally statement) it writes the file with the tokens separated by a comma.


If you look at the documentation for that class' toString() method, it says:
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

So you cannot use that method to fulfill your requirement. How about writing what you are writing to the screen to the file, since that seems to fit the bill?

John Sannito wrote:I tried adding system.out.println() below the System.out.println(hashSetIterator.next()), but that won't even compile.


Java is case-sensitive. "System" is not the same thing as "system".
 
John Sannito
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you cannot use that method to fulfill your requirement. How about writing what you are writing to the screen to the file, since that seems to fit the bill?


John Sannito wrote:I tried adding system.out.println() below the System.out.println(hashSetIterator.next()), but that won't even compile.


I misspoke above. What I did was add out.println(hashSetIterator.next())

right below where I iterated through the HashSet like so:



and comment out the previous write statement out.println(testHash); like so:



And that is what would not compile.
It seems logical that I would be able to write each token to the output file right after I display it to the screen, but I'm stuck on the syntax of how to do that. I couldn't find any help in the Java API for HashSet. I'm still stuck.

 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sannito wrote:
And that is what would not compile.


So what was the compile error?
 
John Sannito
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, it compiles, but doesn't write to the file, and I get the runtime exception:
Exception in thread "main" java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:796)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)
at java112.tests.TestCode.run(TestCode.java:68)
at java112.tests.TestCode.main(TestCode.java:21).

Methinks I should be iterating this BEFORE my finally block closes the FileWriter, which WILL then write to the file if coded this way:



However, I get the same runtime exception:

General Error
java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:796)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)
at java112.tests.TestCode.run(TestCode.java:42)
at java112.tests.TestCode.main(TestCode.java:21)

And I don't really understand what this means. It seems to indicate that at some point there is no iteration of the HashSet when it expects there to be one, but I'm not clear on exactly when that is, or why.
The app writes some of the unique tokens from the input file on separate lines with no other delimiters, but not all of them.
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sannito wrote:

You check for a next element once yet you go next twice. If you need the results of hashSetIterator.next() more than once you must cache it:
 
John Sannito
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duh. I am an idiot. Thanks for the help-sometimes I don't see the forest cuz of all the damn trees in the way......
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!