0

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?

2
  • 1
    You should assemble lists (or arrays) of all the data first and then compare them. Right now, you are only printing the output once if a == d, which is always will because they haven't been assigned a value yet. Commented Jun 25, 2018 at 17:14
  • Thank you sir for this idea. I also think about array. Commented Jun 26, 2018 at 6:45

2 Answers 2

2

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");
           }
     }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir. I have no idea that we can read two files like that in while loop. I think only one file can read at a time. Thanks for this new idea.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.