• Post Reply Bookmark Topic Watch Topic
  • New Topic

SCANNER API and Sorting  RSS feed

 
harrell collins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a scanner reading a comma delimited file. It write three fields to a text file. I need to sort this file while looping through the file. If the first field matches the first field of subsequent lines and the second field does not match the fields if the outer if test I need to increment a counter. Any suggestions?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the goodies already in the JDK library. Turn each line into an object. You mentioned three fields, so you'd probably have a custom class with three fields. Implement the Comparable interface in your class. Then read each line, create an instance of your class, put it in a sorted Set. When you've read them all in, iterate the set and write new lines. You could do your counting as you write.

If any of that didn't make sense, look in the JavaDoc for Comparable, Set, TreeSet.

A couple things could trip you up. Duplicate lines would make identical objects which won't work in Sets. We could get around that. And if you have millions of lines you might run out of memory. That's harder to get around.

Is that enough to go on?
[ September 28, 2006: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!