Results 1 to 2 of 2
  1. #1
    jrnowlan is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default Read CSV File, sort records, and output a new csv file

    Hi, I am new to java so please bear with me. I have a csv file that contains an order details. So there are multiple order numbers repeated for each item on the order.
    Like so:

    15123, EL2060
    15123, EL200F
    15123, EL4030
    15412, EL400B
    15243, EL200F

    What I want to do is create a String with the order number listed once and all the items that go with the order listed after the order number on one line.

    Like so:

    15123,EL2060,EL200F,EL4030
    15412,EL400B
    15243, EL200F

    What I have done so far is read the csv file that contains the records and store them in a hashtable. Then I tried to sort them from there. I am not sure if this is the best approach or not. Again I am an extreme newbie. sorry.

    Here is the code I have to get the records and store them and then write them back out to a new csv file. Any help would be greatly appreciated.


    try {
    Class.forName("org.relique.jdbc.csv.CsvDriver");
    Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + directory );

    Statement stmt = conn.createStatement();

    // Select the ID and NAME columns from sample.csv
    String sqlString = "SELECT order,item FROM Items";


    ResultSet rs = stmt.executeQuery(sqlString);
    Hashtable ht = new Hashtable();
    FileWriter file = new FileWriter("C:\\newFile.csv");
    BufferedWriter out = new BufferedWriter(file);
    StringBuilder sb = new StringBuilder();

    while (rs.next()){

    String key = rs.getString("order");
    String value = rs.getString("item").trim();

    ht.put(key,value);


    String header = "Order" + "," + "Item" + "\n";
    out.write(header);

    sb.append(key + "," + value + "\n" );

    out.write(sb.toString());

    }

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

    Default

    Please explain what your code does and what you need to change about it.
    Print out the results and add comment saying what is wrong.

    What I want to do is create a String with the order number listed once and all the items that go with the order listed after the order number on one line.
    To build a list of items for each order number, you need to check if the order number is in the hashtable and if it is, you need to get its value and add to it the new item.

Similar Threads

  1. Read text file and sort in ArrayList
    By Tabula Rasa in forum New To Java
    Replies: 7
    Last Post: 04-17-2011, 04:08 AM
  2. Replies: 1
    Last Post: 02-04-2010, 09:17 PM
  3. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  4. Replies: 2
    Last Post: 01-26-2009, 04:28 PM
  5. Replies: 1
    Last Post: 01-22-2009, 04:25 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
  •