0

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

newlearner
  • 95
  • 10
  • 1
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jorge Campos Jul 29 '17 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 – newlearner Jul 29 '17 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. – Jorge Campos Jul 29 '17 at 16:51
  • so instead of using == i should .equals let me try – newlearner Jul 29 '17 at 16:53

1 Answers1

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();
        }
    }
}
fg78nc
  • 4,774
  • 3
  • 19
  • 32
  • so i have to replace my inner for loop right?? @fg78nc – newlearner Jul 29 '17 at 17:12
  • 1
    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 – fg78nc Jul 29 '17 at 17:13
  • 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 – newlearner Jul 29 '17 at 17:23
  • 1
    You don't need outer loop, only one loop is enough. – fg78nc Jul 29 '17 at 17:26
  • i took out the other for loop and declare the commonlist like this String[] commonList = null; now it gives me this error error: cannot find symbol commonList.add(database[i]); ^ symbol: method add(String) location: variable commonList of type String[] – newlearner Jul 29 '17 at 17:33
  • error: cannot find symbol .collect(Collectors.toList()); ^ symbol: variable Collectors this the error – newlearner Jul 29 '17 at 17:52
  • 1
    `import java.util.stream.Collectors;` – fg78nc Jul 29 '17 at 17:53
  • error: local variables referenced from a lambda expression must be final or effectively final .filter(x -> Arrays.asList(ad).contains(x)) ^ 1 error – newlearner Jul 29 '17 at 17:59
  • i did try your first version in that i got empty list – newlearner Jul 29 '17 at 18:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150490/discussion-between-fg78nc-and-newlearner). – fg78nc Jul 29 '17 at 18:11