-1

below is my work so far i have used 2 csv files and made 2 different arrays which represents 2 csv file data now what i am want to do that is i want to compare that 2 arrays and print the same vlaues or elements which are same in both arrays this is where i am having difficulty

import java.util.Arrays;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CSVReaderw {

public static void main(String[] args) {

    String csvFile1 = "/Users/Documents/AD.csv";
    String csvFile2 = "/Users/Desktop/database.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    String[] ad = null;
    String[] database = null;

    try {

        br = new BufferedReader(new FileReader(csvFile1));
        while ((line = br.readLine()) != null) {

            // use comma as separator
             ad = line.split(cvsSplitBy);

            //System.out.println("AD [id= " + ad[2] +"]");

        }
        System.out.println("second file result starts here");
        br = new BufferedReader(new FileReader(csvFile2));
        while ((line = br.readLine()) != null) {

           database = line.split(cvsSplitBy);
           //System.out.println("ID =" + database[0]);
        }

        List<String> commonListLambda = Arrays.stream(database)
        .filter(x -> Arrays.asList(ad).contains(x))
        .collect(Collectors.toList());

        System.out.println(commonListLambda);
            // List<String> commonList = new ArrayList<>();
// 
//             for(int i = 0; i < ad.length; i++){
//                 if(Arrays.asList(databse).contains(ad[i]))
//                     commonList.add(ad[i]);
//                  }
//             
//             System.out.println(commonList);



    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

}

Edit: see the updated code as we dicuessed here

4
  • 1
    Possible duplicate of How do I compare strings in Java? Commented Jul 29, 2017 at 16:44
  • in that questions they are only showing how to compare strings i want to comapre the values which is on the arrays and print the same values from arrays Commented Jul 29, 2017 at 16:50
  • 1
    if(ad[i] == database[j]) You are comparing string that you get from the files. It is exactly the same solution. Commented Jul 29, 2017 at 16:51
  • so instead of using == i should .equals let me try Commented Jul 29, 2017 at 16:53

1 Answer 1

1

The problem is that you are rewriting again your array, instead of adding data to it.

while ((line = br.readLine()) != null) {
           database = line.split(cvsSplitBy);

General logic is wrong, you need to compare cell in one file to the cell in other file.

Please see suggested solution, I tried to make code self-explanatory.

public class CSVReaderw {

    public static void main(String[] args) {

        String adDotCsv = "/Users/Documents/AD.csv";
        String databaseDotCsv = "/Users/Desktop/database.csv";
        String line = "";
        String cvsSplitBy = ",";
        BufferedReader br = null;
        List<String> databaseList = new ArrayList<>();
        List<String[]> adList = new ArrayList<>();

        try {
            br = new BufferedReader(new FileReader(databaseDotCsv));

            while ((line = br.readLine()) != null) {
                databaseList.addAll(Arrays.asList(line.split(cvsSplitBy)));
            }

            br = new BufferedReader(new FileReader(adDotCsv));

            line = "";
            while ((line = br.readLine()) != null) {
                adList.add(line.split(cvsSplitBy));
            }

            List<String[]> commonList = new ArrayList<>();

            String cellCInAdFile = null;

            for (String[] rowInAdList : adList) {
                cellCInAdFile = Arrays.toString(rowInAdList).split(",")[2].trim();

                for (String cellAinDatabaseFile : databaseList) {
                    if (cellCInAdFile.equals(cellAinDatabaseFile.trim())) {
                        commonList.add(rowInAdList);
                    }
                }
            }

            for (String[] rowInCommonList : commonList) {
                System.out.println(Arrays.asList(rowInCommonList));
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

so i have to replace my inner for loop right?? @fg78nc
I did not study attention to your code. This is complete solution which will compare two arrays and return List of common elements. Let me know if you need additional help
for(int i = 0; i < ad.length; i++) { for(int j = 0; j < database.length; ++j) { if(!Arrays.asList(ad).contains(database[i])) commonList.add(database[i]); System.out.println(commonList); //value is in both arrays } } this what i did but it's not working gives me cannot find symbol on commonList
You don't need outer loop, only one loop is enough.
import java.util.stream.Collectors;
|

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.