I smell homework here... :)
Okay, so start us off. You have a text file. Can you open it and read lines from it, in a loop? Go ahead and do that, just dumping the lines straight to the screen.
That'll make a good first step.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
i need to read the file and look for any duplicates data/keys in the text file and write
Can you clarify this? Are you trying to eliminate duplicate lines of data, or do you need to parse the lines into data and keys. That would be an added step.
In any case, if you want to display the duplicates only, maybe you should check each item against the rest of the list when you're reading it into the list. If it's already in the list, do whatever you need to do with it - write it to a file, skip adding it to the list, put it in another list, paint it blue and ship it to Waukegan, whatever you like.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Maybe you should check each item against the rest of the list when you're reading it into the list. If it's already in the list, do whatever you need to do with it - write it to a file, skip adding it to the list, put it in another list, paint it blue and ship it to Waukegan, whatever you like.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Or, if you want a second loop, after you've read everything in to the file, you pretty much have to check each item against each other item. Now you're talking about a generic problem of eliminating duplicated items from a list.
The easiest thing to do is to sort the list and go through it - is this item like the one after it? If so, put it in a second list. Write the second list to the file.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Yes, that just goes through and for each item in the ArrayList, it prints it.
That's not what you want, though. For each item in the list, you want to check if it's a duplicate of some other item in the list.
Suppose you have a list of Strings:
blueberry
tangerine
apricot
kiwi
durian
and I give you another String:
gorgonzola
How do you check whether it's in the list?
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
the you can (probably) use a some of Set, that doesn't allows duplicates, and you can test it with methods someSet#contains
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
r0n, do you remember that Disney cartoon of the Sorceror's Apprentice? The one where Mickey Mouse conjures up an endless stream of animated brooms?
I don't know why that came to mind, but maybe you should mark this thread as "closed".
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187