Results 1 to 10 of 10
  1. #1
    tony_pk3 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default Need help with creating a hashset and selecting distinct data

    I have created a java project which reads a text file and calculates the prices based on the salepersons productivity and writes it to a file using hashmap. This is based on there salesid, I need to get the sales person name once and write it separatly to the file. I am trying to use a hashset but haven't figured it out. i can provide code.

    Thanks

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You means that you want to create separate files for each person?

    And yes, it's better to provide more details, so that it's helpful to comment for others.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    writes it to a file using hashmap
    How are you doing that?

  4. #4
    tony_pk3 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    I am new to java, so please have latittude with my terminology. I read the file in using FileReader to a hashmap and wrote the file using fileWriter. I get an output of the names but they are duplicates. I was able to combine multlple entries of the salesid, but not the salesname. I figured i could pull the names in a hashset and put it in a stringbuffer and write it to the same file.

    Current Out put below:
    I just need there names once.

    CARL
    RAY
    LAN
    FRANK
    CARL
    LAN
    RAY


    11 800000.00
    15 20000.00
    16 400000.00
    17 2140000.00
    50 100000.00


    Desired Output below:
    Salesperson
    CARL
    FRANK
    LAN
    RAY

    SalesID Product sales
    11 800000.00
    15 20000.00
    16 400000.00
    17 2140000.00
    50 100000.00

    ////////////////////////////////===============Reads in my file ////////////////////////////////

    FileReader fr = new FileReader("C://Documents and Settings//epeak//My Documents//test.txt");
    BufferedReader br = new BufferedReader(fr);
    String s;
    while((s = br.readLine()) != null) {
    Pattern p = Pattern.compile("[\\s]+");
    String[] str = p.split(s);

    // System.out.println(str[0] + " "+str[1] +" "+str[2]+" "+str[3]);
    Product prop = new Product ();
    prop.setProduct Id(str[0]);
    prop.setProduct Name(str[1]);
    prop.setProduct Value( new Double(str[2]));
    prop.setAgendId(str[3].trim());



    System.out.println( " "+str[1] +" "+str[2]+" "+str[3]);



    if(value.get(str[3].trim())!=null)
    {
    ArrayList pv = (ArrayList)value.get(prop.getAgendId());
    pv.add(prop);




    }
    else
    {
    ArrayList propValue = new ArrayList();
    propValue.add(prop);
    value.put(str[3].trim(),propValue);

    }


    }
    fr.close();

    ///////////////////////////////////////////////////////////////===================Writing my file ////////////////////////////////////////////////////////


    FileWriter fw = new FileWriter("c:/agentreport.txt");
    StringBuffer buf = new StringBuffer();
    StringBuffer buf1 = new StringBuffer();
    StringBuffer buf2 = new StringBuffer();
    DecimalFormat money = new DecimalFormat("0.00");
    String NL = System.getProduct ("line.separator");



    if(value!=null && !value.isEmpty())
    {
    Set set = value.keySet();
    Object[] key = set.toArray();

    Arrays.sort(key);
    System.out.println("test" + Arrays.asList(key));

    for(int i=0;i<key.length;i++)
    {
    // System.out.println(value.get((String)key[i]))
    ArrayList list = (ArrayList) value.get((String)key[i]);

    if(list!=null && !list.isEmpty())
    {
    Iterator iter = list.iterator();
    String productName = "";
    double productValue=0;
    String agentId="";
    while(iter.hasNext())
    // System.out.println(productName);
    {
    Product p = (Product) iter.next();
    if(p!=null)
    {
    productName=p.getProductName().toUpperCase();

    productValue = productValue + p.getProductValue().doubleValue();
    agentId=p.getAgendId();

    }


    buf2.append(" "+ productName);

    buf2.append(NL);
    }

    buf1.append(NL);

    buf1.append( " "+ agentId + " " + money.format(amount) + " ");




    }
    }
    }


    fw.write(buf2.toString());
    fw.write(buf1.toString());
    fw.close();

    }
    Last edited by tony_pk3; 08-24-2010 at 08:35 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Please edit your code and add code tags around it to preserve the formatting. Unformatted code is hard to read. Post all of the code needed to show your problem including the import statements.

    able to combine multlple entries of the salesid, but not the salesname.
    Can you describe what the code is supposed to do, including the contents of the input and what the output files should look like.

  6. #6
    tony_pk3 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    The input file reads in four columns of data to a hashmap. (SalesID, Name, Product and Region).A salesperson may have multiple products sold, so i group the salesids and add the values. Which gives me a single salesid and the total product value. I then write this data to an output file. One column of salesman names and below that two columns, one sales id and the other column is the total amount for that salesid.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    input file reads in four columns of data to a hashmap
    What method do you use to put the four columns of data into the hashmap?
    What are the keys used for each piece of data?

    How many rows are read in from the file? Does each row have the same 4 fields/columns?
    How is the data from one row separated from the data in another row? Do you generate unique keys for each row and column?

    I think we need to see the code. Be sure to put it in code tags and include the import statements

  8. #8
    tony_pk3 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    FileRead method is how i am filling hashmap. Each agent has a a unique salesid. 8 rows are read in and each column is static, but not the data(Region, Salesperson, SalesID, Productvalue). Spaces separtate the data, it is not delimited. The keys are in the file and they are unique to each saleperson.

    By the way how do tags work?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Sorry, I can't help without seeing the code and a small input file.
    Can you explain what you problems are?

    By the way how do tags work?
    To use code tags, put [code ] before and [/code ] after your code (without the space)

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 2
    Last Post: 05-01-2010, 09:16 PM
  2. Replies: 3
    Last Post: 08-20-2009, 02:04 AM
  3. Replies: 0
    Last Post: 01-03-2008, 12:28 PM
  4. Help Creating A Graph From Inputted Data
    By adlb1300 in forum New To Java
    Replies: 6
    Last Post: 10-28-2007, 04:45 AM
  5. Creating a stack with data
    By ai_2007 in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 03:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •