USB
USB

Reputation: 6139

Comparing lines in a file

I am trying to compare File 1 and File 2.

File 1:

7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1
7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 1
7.3 0.25 0.39 6.4 0.034 8 84 0.9942 3.18 0.46 11.5 5 1
6.9 0.38 0.25 9.8 0.04 28 191 0.9971 3.28 0.61 9.2 5 1
5.1 0.11 0.32 1.6 0.028 12 90 0.99008 3.57 0.52 12.2 6 1

File 2:

5.1 0.11 0.32 1.6 0.028 12 90 0.99008 3.57 0.52 12.2 6 -1
7.3 0.25 0.39 6.4 0.034 8 84 0.9942 3.18 0.46 11.5 5 1
6.9 0.38 0.25 9.8 0.04 28 191 0.9971 3.28 0.61 9.2 5 -1
7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 -1
7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1

In both files the last element in each line is class label.

I am comparing if the class labels are equal. ie compare the classlabel of

line1:7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1

with

line2:7.3 0.28 0.36 12.7 0.04 38 140 0.998 3.3 0.79 9.6 6 1

Matches.

compare

line1:7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 1

with

line2:7.4 0.33 0.26 15.6 0.049 67 210 0.99907 3.06 0.68 9.5 5 -1

Not matches

Updated

What I did is

String line1;
String line2;
int notequalcnt = 0;
while((line1 = bfpart.readLine())!=null){
found = false;
while((line2 = bfin.readLine())!=null){               
     if(line1.equals(line2)){
          found = true;
      break;
     }
   else{
    System.out.println("not equal");
    notequalcnt++;
   }
}

}

But I am getting every one as not equal.

Am I doing anything wrong.

Upvotes: 4

Views: 172

Answers (4)

thep
thep

Reputation: 68

After the first iteration itself, line2 becomes null. So, the loop will not execute again... Declare line2 buffer after the first while loop. Use this code:

public class CompareFile {

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

   String line1;
   String line2;
   boolean found;
   int notequalcnt =0;

   BufferedReader bfpart = new BufferedReader(new FileReader("file1.txt"));

   while((line1 = bfpart.readLine())!=null){
    found = false;
    BufferedReader bfin = new BufferedReader(new FileReader("file2.txt"));
    while((line2 = bfin.readLine())!=null){

        System.out.println("line1"+line1);
        System.out.println("line2"+line1);
        if(line1.equals(line2)){
            System.out.println("equal");

            found = true;
            break;

        }
        else{
            System.out.println("not equal");

        }
    }
    bfin.close();

    if(found==false)
      notequalcnt++;
    }
    bfpart.close();
  }
}

Upvotes: 2

ray
ray

Reputation: 457

If the target is to compare and find the matching lines. Convert the file contents to an arraylist and compare the values.

Scanner s = new Scanner(new File("file1.txt"));
ArrayList<String> file1_list = new ArrayList<String>();
while (s.hasNext()){
    file1_list .add(s.next());
}
s.close();

s = new Scanner(new File("file2.txt"));
ArrayList<String> file2_list = new ArrayList<String>();
while (s.hasNext()){
    file2_list .add(s.next());
}
s.close();

for(String line1 : file1_list ){
    if(file2_list.contains(line1)){
       // found the line
    }else{
        // NOt found the line
    }
}

Upvotes: 0

Jason C
Jason C

Reputation: 40315

You're comparing every line from file 1 with every line from file 2, and you are printing "not equal" every time any one of them doesn't match.

If file 2 has 6 lines, and you are looking for a given line from file 1 (say it's also in file 2), then 5 of the lines from file 2 won't match, and "not equal" will be output 5 times.

Your current implementation says "if any lines in file 2 don't match, it's not a match", but what you really mean is "if any lines in file 2 do match, it is a match". So your logic (pseudocode) should be more like this:

for each line in file 1 {
   found = false
   reset file 2 to beginning
   for each line in file 2
      if line 1 equals line 2
          found = true, break.
   if found
      "found!"
   else
      "not found!"
}

Also you describe this as comparing "nth line of file 1 with nth line of file 2", but that's not actually what your implementation does. Your implementation is actually comparing the first line of file 1 with every line of file 2 then stopping, because you've already consumed every line of file 2 in that inner loop.

Your code has a lot of problems, and you probably need to sit back and work out your logic on paper first.

Upvotes: 1

Related Questions