Results 1 to 6 of 6
  1. #1
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default seperation of file contents

    hi
    i need some logic to solve a problem there are two array list.i have to compare the two array list and write the similar lines in both list and non similar lines in both the list in two separate files.

    list 1 contains these lines from a file
    ename eid evalue
    S 01 S425
    P 02 P637
    C 03 C826

    list2 contains these lines from another file
    ename eid evalue
    P 02 P637
    E 04 E738
    D 05 D369

    the o/p file that should contain similar one should have
    ename eid evalue
    P 02 P637

    the o/p file that should contain dissimilar ones should have
    ename eid evalue
    S 01 S425
    C 03 C826
    E 04 E738
    D 05 D369

    and how do i go about having "ename eid evalue" in both the files..

    any suggestions please..

  2. #2
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    The code which i have tried is here:
    Java Code:
    import java.io.*;
    import java.util.*;
    
    class comparef1
    {
       public static void main(String args[])throws IOException
       {
         try{
         String line1;
         String line2;
         ArrayList<String> list1=new ArrayList<String>();
         ArrayList<String> list2=new ArrayList<String>();
         ArrayList<String> list_similar=new ArrayList<String>();
         ArrayList<String> list_disimilar=new ArrayList<String>();
         
         		BufferedReader br1=new BufferedReader(new FileReader("File1.txt"));
          		while((line1=br1.readLine())!=null)
           		{
             	list1.add(line1);
                    //System.out.println(list1);
              	}
                    
                    
         		BufferedReader br2=new BufferedReader(new FileReader("File2.txt"));
          		while((line2=br2.readLine())!=null)
           		{
             	list2.add(line2);
              	//System.out.println(list2);
            	}
    
                    for(int i=0;i<list1.size();i++)
                     {
                      for(int j=0;j<list2.size();j++)
                       {
                         if(list1.get(i).equals(list2.get(j)))
                           {
                          list_similar.add(list1.get(i));
                           list1.remove(i);
                           
                           }
                         else
                           {
                           list_disimilar.add(list1.get(i));
                           list_disimilar.add(list2.get(j));
                            }
                        
                        }
                      }
           		System.out.println(list_similar);
                    System.out.println(list_disimilar);
                   
           }
         catch(FileNotFoundException e)
           {
             System.out.println(e);
             }
         
         }
    }
    can anyone please give me a lead to solve this problem

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

    Default

    One way to see if the contents of one file is in the other file is to use a hashtable. Read all of one file into the hash table and then read the other file and ask the hashtable if it has that record.
    Use the value part of stored data in the hashtable to keep track of the number of matches found. At eof on the second file, get all the values in the hashtable and see which ones had matches or not.


    Could you put some comments in your code showing your logic?
    If you sorted the list of records saved in the arrays, you could do something like a merge in your nested loop to compare the contents of one array with another.

    What problem are you having? Copy and post error messages here.
    Last edited by Norm; 07-11-2008 at 08:27 PM.

  4. #4
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    this is the output i am getting..i could get the similar contents

    [ename eid evalue, P 02 P637]

    but for dissimilar contents i get this..

    [S 01 S425, P 02 P637, S 01 S425, E 04 E738, S 01
    S425, D 05 D369, P 02 P637, ename eid evalue, C 03 C826, E
    04 E738, C 03 C826, D 05 D369]

    i know the problem is with the logic..i am not able to figure this out..i am working on it..but if u have any other suggestion please kindly let me know..

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

    Default

    Don't understand what the output you posted means.
    it looks like the contents of some arrays, but there are no comments with it to describe what they are.
    Add a coment to the println("NOT FOUND: " + array); etc
    Simplify the problem by shortening the input files to 2 records each. For example:
    File 1)
    Same1
    diff1
    File 2)
    Same1
    diff2
    Then add enough println() statements to your code to see exactly how it is processsing each record. From that you should be able to see what is going wrong.

  6. #6
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    Thanks Norm i will do that..your Hashset idea seems to be working..i am working on it....

Similar Threads

  1. Replies: 9
    Last Post: 07-10-2008, 04:52 PM
  2. Viewing contents of zip file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-03-2008, 05:16 PM
  3. Concatenation file contents
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-07-2008, 01:29 PM
  4. Reading file contents (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 09:00 AM
  5. Viewing contents of JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 03:12 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
  •