I have two data file. Data are double type. File one consist of 3 column and several Rows and File two consist of 4 Column and Several row. I read data from both file separately in one Java program and the Column one data of File1 are matched with the Column one data of File2 then a message will display that data are matched else data are not match. My code is like that
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:\\R5_M.txt"));
Scanner X =new Scanner(new File("C:\\R5_O.txt"));
double a=0.0,b=0.0,c,d=0.0,e=0.0,f,g,h;
if(a==d) {
while (X.hasNext()) {
a = X.nextDouble();
b = X.nextDouble();
c = X.nextDouble();
}
while (Y.hasNext()) {
d = Y.nextDouble();
e = Y.nextDouble();
f = Y.nextDouble();
g = Y.nextDouble();
}
System.out.println("They are matched");
}
else{
System.out.println("Not Matched");
}
}
}
It's output is They are matched
for only one time. But it should write output equal to number of rows. If I have 10 Rows in both data files and 6 data are matched and 4 are not matched then I get a output They are matched for 6 times and Not Matched for 4 times.
One obvious reason is both while loops are end within there scope.So the value that change for a,b,c,d,e,f,g are remain in the scope or should I say they are overwrite in each iteration and if I call a and d outside while loop then it returns only the last value in this case 10th value. So where should I write down the if statement to compare each value?
You need to do like this
public static void main(String args[])throws Exception{
Scanner Y =new Scanner(new File("C:\\R5_M.txt"));
Scanner X =new Scanner(new File("C:\\R5_O.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();
b = X.nextDouble();
c = X.nextDouble();
d = Y.nextDouble();
e = Y.nextDouble();
f = Y.nextDouble();
g = Y.nextDouble();
if(a==d) {
System.out.println("They are matched");
}
else{
System.out.println("Not Matched");
}
}
One efficient method is to use a map to store each entry from both files and then compare the two:
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner Y = new Scanner(new File("C:\\R5_M.txt"));
Scanner X = new Scanner(new File("C:\\R5_O.txt"));
double a = 0.0, b = 0.0, c, d = 0.0, e = 0.0, f, g, h;
// Create a map to hold the values from each file
HashMap<String, Double> yList = new HashMap<>();
HashMap<String, Double> xList = new HashMap<>();
// Read in the data from both files
while (Y.hasNext()) {
// This will place the values into the HashMap. The first value is whatever "key"
// you want to use, the second is the value itself.
yList.put("a", Y.nextDouble());
yList.put("b", Y.nextDouble());
yList.put("c", Y.nextDouble());
}
while (X.hasNext()) {
xList.put("a", X.nextDouble());
xList.put("b", X.nextDouble());
xList.put("c", X.nextDouble());
}
// Now, you can compare values in both maps
// The HashMap has a list called entrySet that allows you to iterate over all
// of the entries in the list
for (Map.Entry<String, Double> entry : yList.entrySet()) {
// This will check if the current entry's key/value pair matches the identical
// key/value pair in the xList map.
if (entry.getValue() == xList.get(entry.getKey()).doubleValue()) {
System.out.println(entry.getKey() + " matches!");
} else {
System.out.println(entry.getKey() + " does NOT match!");
}
}
}
}
This can be adapted to read an unknown number of entries from each file as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.