Results 1 to 12 of 12
  1. #1
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default Update arraylist based on some conditions

    I am working on a project where I need to read a text file line by line and if some conditions met write the data into one line.

    The approch I am researching is to use an arraylist with binary search and if search element found to write an updated data to a new file.

    I am looking for any directions on how best to tackle this task.


    Here is sample of the data:

    ID1,NAME1,DATE1,TIME1
    ID1,NAME1,DATE1,TIME2
    ID1,NAME1,DATE1,TIME3
    ID2,NAME2,DATE1,TIME4

    Expected result:
    ID1,NAME1,DATE1,TIME1,TIME2,TIME3
    ID2,NAME2,DATE1,TIME4

    thanks
    Last edited by emyk; 04-11-2011 at 10:30 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    You haven't really defined a question...are you having trouble reading the file(s)? Using a List? Parsing the information?
    I am working on a project where I need to read a text file line by line and if some conditions met write the data into one line.
    Any reason to read the file into memory, and not just parse it as you read it? Read the file line by line, parse, and write out as appropriate.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    If that data is a good representation of what you are working with then then ID, Name and Date are a key (in my mind) into a Map, that has a List<Time> as its value. So read each line and then add it to the map/list.

  4. #4
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Thanks Tolls - yes the data presentation is accurate, so I will explore map/list suggestion.
    thanks.

  5. #5
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    I used an online example that uses Hashmap. It reads a file one line at a time and attempt to parse the data into the hash map. But when listing the key and the value from the Hashmap, not getting expected result (add to the hash map logic is where I am being challenged)

    Here is the code i am working on:

    public static void main(String[] args) throws IOException {

    String pFile = "C:/File.csv";
    BufferedReader in;
    try {
    in = new BufferedReader(new FileReader(pFile));
    } catch (FileNotFoundException ex)
    { System.out.println("File Not Found"); return; }
    //2.Declare the HashMap:
    HashMap<String, String> map = new HashMap<String, String>();
    //3.Read lines, Parse them and add to Hash-Map:
    String line;
    //loop till end of file
    while((line = in.readLine()) != null)
    {
    //remove spaces from front and back...
    line = line.trim();
    //ignore if line is empty
    if(line.isEmpty()) continue;
    //split line into array
    String[] p=line.split(",");
    String ID = p[4];
    String NAME = p[5];
    String DATE = p[7];
    String TIME = p[8];
    //Parse the string.
    String property = String.valueOf(p[4]);
    String property1 = String.valueOf(p[5]);
    String property2 = String.valueOf(p[7]);
    String property3 = String.valueOf(p[8]);
    //Check if hash already contains key.
    if(!map.containsKey(ID)) {
    if(!map.containsKey(DATE)){

    map.put(ID, map.get(ID) + "," + property + map.get(DATE) + "," + property2 + "," + map.get(NAME) + "," + property1 + "," +map.get(TIME) + "," + property3);

    }}
    //if not then add the new key with new value

    else {
    map.put(TIME,map.get(TIME) + "," + property3);
    }
    }
    Iterator iterator = map.keySet().iterator();

    while (iterator.hasNext()) {
    String key = iterator.next().toString();
    String value = map.get(key).toString();

    System.out.println(key + " " + value);
    }

    }
    }

    The out put I get when I run this program is as follow:

    0200 null,0200
    400 null,400
    104 null,104null,20110403,null,Armstrong,null,0900
    103 null,103null,20110403,null,Adams,null,800

    Expected output is:

    104,20110403,Armstrong,0900,0200
    103,20110403,Adams,800,400

    Here is content of the File.csv
    1,Jacksonville,Florida,100,103,Adams,Kevin,2011040 3,800
    1,Jacksonville,Florida,100,103,Adams,Kevin,2011040 3,400
    1,Jacksonville,Florida,200,104,Armstrong,Mary,2011 0403,0900
    1,Jacksonville,Florida,200,104,Armstrong,Mary,2011 0403,0200
    Last edited by emyk; 04-12-2011 at 08:28 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Please use code tags.
    Java Code:
    map.put(ID, map.get(ID) + "," + property + [B][U]map.get(DATE)[/U][/B] + "," + property2 + "," + [B][U]map.get(NAME)[/U][/B] + "," + property1 + "," +[B][U]map.get(TIME)[/U][/B] + "," + property3);
    What is this supposed to be doing? Because the three highlighted bits will all return null. Your map is keyed by ID, not by any of the other bits.

    ETA:
    Create a class that represents the data in a single line.
    Read in a line, and create an instance of that class using the data from the line.
    Use this object as the Key (that will involve creating equals and hashcode methods in the class, based on ID/Name/Date).
    Use a List<String> as the value, which will hold the Time properties.
    Last edited by Tolls; 04-13-2011 at 08:21 AM.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    other point: when you insert a value in a hasmap with a key that is already in the map the value will be replaced with the new one. so if your data have the same id1, id1 only the last one will be in the map.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    That's why there's a List.
    Find in Map. If exists then add to List, else add new List.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by emyk View Post
    Thanks Tolls - yes the data presentation is accurate, so I will explore map/list suggestion.
    thanks.

    If what was posted was an accurate representation of the input then there's no reason for any elaborate collection. Just use a single loop: if the line matches the start of the previous line just output the date part, if not output '\n' followed by the the whole line (minus its line terminator).

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Good point.
    I didn't even think they might be in order to begin with.

  11. #11
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Thanks Tolls for pointing me to the right direction. I will also validate if the data presentation is accurate (always sorted by name and date) so that I can use a single loop as pbrockway2 and dowhile pointed.

    thanks.
    Last edited by emyk; 04-13-2011 at 01:35 PM.

  12. #12
    emyk is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Thanks all for your suggestions. Here is my working script.


    // Create file
    FileWriter fstream = new FileWriter("C:/NewText.txt",true);
    BufferedWriter out = new BufferedWriter(fstream);


    //Get the object of DataInputStream
    FileInputStream fstream1 = new FileInputStream("C:\\Text.csv");
    // Get the object of DataInputStream
    DataInputStream in1 = new DataInputStream(fstream1);
    BufferedReader br = new BufferedReader(new InputStreamReader(in1));
    String strLine;
    //Read File Line By Line
    ArrayList arraylist = new ArrayList();

    while ((strLine = br.readLine()) != null) {

    // Print the content on the console
    String[] values = strLine.split(",");
    String EMP_ID = values[4];
    String DATE = values[7];
    String TIME = values[8];
    String EmpDateTime = EMP_ID + "," + DATE + "," + TIME;
    String EmpDate = EMP_ID + "," + DATE;

    int index = arraylist.indexOf(EmpDate);
    if(index == -1){
    System.out.println(EmpDate + " does not contain ArrayList ");
    arraylist.add(EmpDate);
    out.write( "\n");
    out.write( EmpDateTime);
    }
    else{

    out.write( "," + TIME );
    }
    }
    out.close();

    }

    }

Similar Threads

  1. update arraylist to mysql database
    By sks in forum JDBC
    Replies: 11
    Last Post: 04-01-2011, 08:26 AM
  2. Replies: 0
    Last Post: 03-19-2011, 11:54 PM
  3. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  4. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 10:07 AM
  5. [SOLVED] is ArrayList zero based indexing?
    By Nicholas Jordan in forum Advanced Java
    Replies: 5
    Last Post: 07-13-2008, 06:24 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
  •