Question

Im trying File comparison in Java. How do I search for string in the next occuring line(ignoring whitespace)

Eg:

       **File1**                **File2**
     <name>abc</name>  |   <name>abc</name>              Equal
        <age>21</age>  |                                 Not Equal
<company>zyz</company> |   <age>21</age>                 Not Equal
                       |   <company>zyz</company>        Not Equal

Currently with my logic, I've a string with CSV

<name>abc</name>, <age>21</age>, <company>zyz</company>, ####Start of target File####,
<name>abc</name>,              , <age>21</age>, <company>zyz</company>

Problem: In the second line of CSV the age value is blank. When my program checks the second line in File1 and second line in File2 they are not equal hence it gives false for the corresponding lines as well.

What needs to be achieved: I must ignore white space and check for the next occuring value, if both are equal then move the second line of file1 down.

The output must look like this

       **File1**                **File2**
     <name>abc</name>  |   <name>abc</name>
                       |
        <age>21</age>  |   <age>21</age>
<company>zyz</company> |   <company>zyz</company>

here is what I've tried so far

public List<String> FileCompare(String source, String target) {
try {
    //String source="D:/reference.xml";
    //String target="D:/comparison.xml";
    //Diff d=new Diff(myControlXML, myTestXML);
    FileReader fr = new FileReader(source); 
    FileReader fr1 = new FileReader(target);

    BufferedReader br = new BufferedReader(fr);
    BufferedReader br2 = new BufferedReader(fr1);

    String s1,s2;

    String st= new String();
    String st2= new String();


    while((s1 = br.readLine()) != null) { 
            myList.add(s1);
            st=st.concat(s1);   
            //System.out.println(s1);
    } 

    Collections.addAll(myList, "#########Start of target#########");

    while((s2 = br2.readLine())!=null){

        st2=st2.concat(s2);

        myList1.add(s2);
    }
    myList.addAll(myList1);
    System.out.println(myList);
    //System.out.println(myList1);
}
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return myList;
}

Here is the main Code

    Compare c=new Compare();
//FileCompare returns CSV string
List<String> s=c.FileCompare("D:/reference.xml", "D:/comparison.xml"); 

String pdf=s.toString();
String[] tokens=pdf.split(",");
for(String token:tokens)
    System.out.println(token);

How do I go about doing this by ignoring whitespace? Please help

many Thanks!

Was it helpful?

Solution

Both of your loops concat a String if it's not null, without checking if it's actually full of whitespaces. Try the following to prevent appending blank lines:

s1 = br.readLine();
while(s1 != null) { 
    if(s1.trim().length() > 0) {
        myList.add(s1);
        st=st.concat(s1);   
        //System.out.println(s1);
    }
    s1 = br.readLine();
}

The same goes for s2 and his loop.

OTHER TIPS

I guess you are splitting the file into comma separated values.So you have two arrays of comma separated value, each corresponding to particular file. Compare the first value from both array.If they are equal,increment counter for both the arrays.If you encounter empty space for any entry,increment that counter alone.If you are encountering empty entry in both the arrays, increment both.

I hope you got the algo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top