我有两个数据文件。数据是双精度类型(例如90.0、25.63)。File1由3列和几行组成,而File2由4列和几行组成。我在一个Java程序中分别从两个文件中读取数据,并且File1的Column1数据与File2的Column1数据匹配,然后一条消息将显示数据已匹配,否则数据不匹配。第一个数据文件中的行数与第二个数据文件中的行数不同。如果在第4行找到匹配项,则第4行之前的数据将按原样写入,而在第4行将记录下来There is match。
例子:
文件1:
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
文件2:
5.0 0.8974 1.2358 0.2581
7.0 0.3258 0.6528 0.6987
File2中的行数低于File1中的行数。File1和File2的第一列的所有数据都按升序排列。
我想写2.0并且3.0因为它是。然后在File2中找到一个匹配项,因此它将写入“ Match found”,然后6.0从File1中按原样写入。然后再次搜索是否找到匹配项,然后再次写下“找到匹配项”。
代码:
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");
}
}
}
}
我需要执行任何搜索程序吗?喜欢Binarsearch?在Java中有一个过程Arrays.Binarysearch(array,key)。因此,要实现这一点,我需要将a变量存储在数组中。然后,将该数组的每个单元格与进行比较d。那是正确的程序吗?
慕容3067478
相关分类