Search code examples
javaarraysfilebinary-search

Multiple File reading where number of rows are not equal


I have two data files. Data are double type (e.g., 90.0, 25.63). File1 consists of 3 columns and several rows and File2 consists of 4 columns and several rows. I read data from both files separately in one Java program and the Column1 data of File1 is matched with the Column1 data of File2, then a message will display that data is matched else data is not matched. The number of rows in the first data file is not same of the number of rows of the second data file. If there is a match found in row number 4 then the data before 4th row will write as it is and in fourth row it will write down There is match.

Example:

File1:

 2.0   0.6258  0.239  1.852
 3.0   0.5289  0.782  2.358
 5.0   1.2586  2.3658 0.1258
 6.0   0.235   0.8547 3.5870

File2:

5.0  0.8974  1.2358  0.2581  
7.0  0.3258  0.6528  0.6987

Number of rows in File2 is lower than in File1. All the data of 1st column for both File1 and File2 is in ascending order.

I want to write 2.0 and 3.0 as it is. Then there is a match found from File2 so it will write "Match found", then 6.0 from File1 is written as it is. Then again searching if a match is found, then again write down "Match found".

Code:

import java.io.File;
import java.util.Scanner;

public class F1 {
    public static void main(String args[])throws Exception{
        Scanner Y =new Scanner(new File("C:\\File1.txt"));
        Scanner X =new Scanner(new File("C:\\File.txt"));
        double a=0.0,b=0.0,c,d=0.0,e=0.0,f,g,h;
        while (X.hasNext() && Y.hasNext()) {
            a = X.nextDouble();
            System.out.println(a);//1st row of file1,2nd row of file1.... So lastly some rows at the end of the file will be discard
            b = X.nextDouble();

            c = X.nextDouble();

            d = Y.nextDouble();
            System.out.println(e);// 1st row of file2. Every row print as the number of row is less than File1.
            e = Y.nextDouble();

            f = Y.nextDouble();
            g = Y.nextDouble();

            if(a==d) {
                System.out.println("They are matched");
            }
            else{
                System.out.println("Not Matched");
            }
        }
    }
}

Do I need to implement any search procedure? Like Binarsearch? In Java there is a procedure Arrays.Binarysearch(array,key). So to implement that I need to store the a variable in an array. Then each cell of this array will be compared with d. Is that correct procedure?


Solution

  • The approach you have chosen will work if the files are either sorted by the first column or are otherwise guaranteed to have the same order of elements.

    If that is not the case you must first read the files into memory (e.g. a List<Double> per file) and compare the contents of the list, for example by sorting the lists and then comparing element for element (while remembering that floating point comparisons might be tricky). If the values are guaranteed to be unique per file you could use a Set per file and directly compare the sets.