Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2008, 05:57 PM
Member
 
Join Date: Jun 2008
Posts: 74
Rep Power: 0
Ms.Ranjan is on a distinguished road
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..
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-11-2008, 07:57 PM
Member
 
Join Date: Jun 2008
Posts: 74
Rep Power: 0
Ms.Ranjan is on a distinguished road
Default
The code which i have tried is here:
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-11-2008, 09:24 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
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 09:27 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-11-2008, 09:50 PM
Member
 
Join Date: Jun 2008
Posts: 74
Rep Power: 0
Ms.Ranjan is on a distinguished road
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..
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-11-2008, 10:01 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-11-2008, 10:17 PM
Member
 
Join Date: Jun 2008
Posts: 74
Rep Power: 0
Ms.Ranjan is on a distinguished road
Default
Thanks Norm i will do that..your Hashset idea seems to be working..i am working on it....
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
swapping the contents of the file and writing to another file Ms.Ranjan New To Java 9 07-10-2008 05:52 PM
Viewing contents of zip file Java Tip Java Tips 0 03-03-2008 06:16 PM
Concatenation file contents Java Tip Java Tips 1 02-07-2008 02:29 PM
Reading file contents (BufferedReader) Java Tip Java Tips 0 02-07-2008 10:00 AM
Viewing contents of JAR file Java Tip Java Tips 0 12-21-2007 04:12 PM


All times are GMT +2. The time now is 05:57 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org