Comparing strings 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 on each line is the class label.

I compare if class labels are equal. those. compare class label

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

from

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

from

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

Does not match

Update

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 get it like that not equal.

I am doing something wrong.

+4
source share
4 answers

After the first iteration, line2 becomes zero. Thus, the loop will not execute again ... Declare the 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();
  }
}
+2
source

1 2, " " , - .

2 6 , 1 (, 2), 5 2 , " " 5 .

: " - 2 , ", , , " 2 , ". , () :

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!"
}

"n- 1 n- 2", , . 1 2, , 2 .

, , , .

+1

If the goal is to compare and search for matching strings. Convert the contents of the file to 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
    }
}
0
source

Source: https://habr.com/ru/post/1532595/


All Articles