Welcome to Dream.In.Code
Become a Java Expert!

Join 150,413 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,016 people online right now. Registration is fast and FREE... Join Now!




HashSet

 
Reply to this topicStart new topic

HashSet

bigdoggy
3 Apr, 2008 - 07:35 AM
Post #1

D.I.C Head
**

Joined: 31 Oct, 2007
Posts: 115


My Contributions
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 smile.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: HashSet
3 Apr, 2008 - 08:09 AM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
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 smile.gif


- 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);
}
}

User is offlineProfile CardPM
+Quote Post

bigdoggy
RE: HashSet
3 Apr, 2008 - 08:32 AM
Post #3

D.I.C Head
**

Joined: 31 Oct, 2007
Posts: 115


My Contributions
Thanks for taking the time to help me out. biggrin.gif

This post has been edited by bigdoggy: 3 Apr, 2008 - 08:33 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:01PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month