Reputation: 15
I'm attempting to read each line within an .inp file and for every non-duplicate, write the line to a new file. The issue I'm running into with the code I have so far is that all lines are written into the output file, regardless of if they're duplicates of previous line(s) or not. I'm using a Scanner object to read the file and a BufferedReader/FileWriter object to write the output file.
How do I avoid writing the duplicates?
String book = reader.nextLine();
boolean duplicate = false;
while (reader.hasNext() == true) {
try {
duplicate = reader.hasNext(book);
if (duplicate == true) {
book = reader.nextLine();
} else {
writer.write(book + "\n");
book = reader.nextLine();
}
} catch (NoSuchElementException ex) {
break;
}
}
Upvotes: 1
Views: 930
Reputation: 97282
Depending on the situation:
HashSet
and upon processing a line check whether the set already contains()
the line or not.HashSet
, store a hash (e.g. SHA1) of each line, and compare against that.(*) Relative to available memory
Upvotes: 1