QUOTE(bigdoggy @ 3 Apr, 2008 - 08:35 AM)

Hi all,
I am trying to write a program that takes a list of peoples names and outputs any duplicate names along with how many total duplicates there were , using a HashSet
CODE
public class DuplicateVoters
{
public static void main(String [] args)
{
//create a new HashSet to store the voter identities
HashSet<String> voterIdentity = new HashSet<String>();
String line;
BufferedReader in=new BufferedReader(new FileReader(args[0]));
PrintWriter out=new PrintWriter(new FileWriter(args[1]));
int noOfDuplicates =0;
while((line = in.readLine()) != null)
voterIdentity.add(line);
{
for(int index=0;index<=voterIdentity.size()-2;index+=2)
if(! voterIdentity.add(line))
{
out.println(voterIdentity(index));
noOfDuplicates ++;
}//if
out.println("The number of duplicates found was "+noOfDuplicates);
in.close();
out.close();
}//while
Please can anyone help with this problem

- a badly placed {
- try and catch statements are missing
- don't see why you are reading back the HashSet, the add method will return false if duplicate
- what is the method voterIdentity(index) ?
this one might be simpler.. the try and catch are bonus
java
public class DuplicateVoters {
public static void main(String [] args)
{
HashSet<String> voterIdentity = new HashSet<String>();
String line;
int noOfDuplicates =0;
try {
BufferedReader in=new BufferedReader(new FileReader(args[0]));
PrintWriter out=new PrintWriter(new FileWriter(args[1]));
while((line = in.readLine()) != null) {
if(!voterIdentity.add(line)) {
noOfDuplicates++;
out.println(line);
}
}
in.close();
out.close();
}
catch(IOException e) {
System.out.println("Exception opening/writing/closing file: " + e);
}
System.out.println("The number of duplicates found was "+noOfDuplicates);
}
}