-1

I'm trying to compare two files line by line. Let's say for example:

File1

a,b,c,d,12
e,f,g,h,20
h,g,t,y,30

File2

f,g,h,j,30
e,h,j,f,50
a,b,c,d,60
e,f,g,h,70

I want my output to be like:

a,b,c,d,12,50
e,f,g,h,20,70

I've written this code:

while ((line1 = bufferedFormattedReaderMaster.readLine()) != null) {

    temp1 = line1.substring(0, 7);
    temp3 = line1.substring(8, 10);

    while ((line2 = bufferedFormattedReaderAnalytical.readLine()) != null) {

        temp2 = line2.substring(0, 7);
        temp4 = line2.substring(8, 10);

        if (temp1.equals(temp2)) {

            System.out.println("OH YES");
            bufferedWriterFinalResults.write(temp1 + ","  +temp3 + "," + temp4);
            bufferedWriterFinalResults.newLine();
            numberOfFinalResults++;

        }   
    }
}

But my output is only:

a,b,c,d,12,50 

and not the next line.

Jonah Graham
  • 7,890
  • 23
  • 55
Ravi Krishna
  • 67
  • 1
  • 9

1 Answers1

0

The bufferedFormattedReaderMaster and bufferedFormattedReaderAnalytical can be read only one time. Brush up your skills on I/O. I did a simple list based conversion of your code. The code may not compile but this roughly what you have to implement. There are better ways as well.

List<String> bufferedFormattedReaderMasterList =new ArrayList<String>();
List<String> bufferedFormattedReaderAnalyticalList =new ArrayList<String>();
    while ((line1 = bufferedFormattedReaderMaster.readLine()) != null) {

                   bufferedFormattedReaderMasterList.add(line1 );
                }


    while ((line2 = bufferedFormattedReaderAnalytical.readLine()) != null) {

                   bufferedFormattedReaderAnalyticalList.add(line2);
                }



for(String line1:bufferedFormattedReaderMasterList) {

                    temp1 = line1.substring(0, 7);
                    temp3 = line1.substring(8, 10);

                    for(String line2:bufferedFormattedReaderAnalyticalList) {

                        temp2 = line2.substring(0, 7);
                        temp4 = line2.substring(8, 10);

                        if (temp1.equals(temp2)) {

                            System.out.println("OH YES");
                            bufferedWriterFinalResults.write(temp1 + ","  +temp3 + "," + temp4);
                            bufferedWriterFinalResults.newLine();
                            numberOfFinalResults++;

                        }   
                    }
                }
Ashraff Ali Wahab
  • 1,088
  • 9
  • 19