2

I'm trying to check if a row of a csv file is present in another csv file. My code is the following:

public void readIncassiOLG(String filename,List<String []> itemsToCheck){
    CSVReader reader = new CSVReader(new FileReader(filename),CSV_SEPARATOR);
    List<String []> body = reader.readAll();
    for(String[] item : itemsToCheck){
        if(body.contains(item)){
            System.out.println("Item present");
        }
        else{
            System.out.println("Item not present");
        }
    }
}

items to check (itemsToCheck) are the rows filtered from first file, I checked that there are some row in both files, but the following method print me always "Item not present".

Any suggestion about this?

David
  • 2,987
  • 1
  • 29
  • 35
Skizzo
  • 2,883
  • 8
  • 52
  • 99
  • 1
    print out the values, both of your array and your list, and check whether corresponding elements are there. There might be a small difference you are overlooking. – Stultuske May 18 '15 at 12:58

4 Answers4

4

The List's contains method returns true if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

In your case, it will compare 2 String arrays. However , array1.equals(array2) is the same as array1 == array2.

This means it will check if it is the same array. To compare the contents of the arrays you need something like:

Arrays.equals(array1, array2)

Therefore the list's contains method is not suited for your use (i.e. comparing the array contents) but rather the equals method of the java.util.Arrays or similar.

Laurentiu L.
  • 6,566
  • 1
  • 34
  • 60
0

You need to make sure that the string is trimmed & is checked with a method similar to equalsIgnoreCase(). Basically, the actual 'contents' is not being checked in these arrays - mainly for body. Rework this array, trim the strings, create a fresh array & then you will be able to compare it with item - in a single loop/pass.

Raúl
  • 1,542
  • 4
  • 24
  • 37
0

For what you are trying to do I would suggest you try the deepEquals() in java.
https://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#deepEquals(java.lang.Object,%20java.lang.Object)

Scott Conway
  • 975
  • 7
  • 13
0

From Java documentation

public boolean contains(Object o) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

To keep it simple (even if it may appear not very elegant) you should change your code as follow:

public void readIncassiOLG(String filename, List<String []> itemsToCheck) {
    CSVReader reader = new CSVReader(new FileReader(filename), CSV_SEPARATOR);
    List<String []> body = reader.readAll();
    for (String[] item : itemsToCheck) {
        for (String[] bodyItem : body) {
            if ( Arrays.equals(bodyItem, item) ) {
                System.out.println("Item present");
            } else{
                System.out.println("Item not present");
            }
        }
    }
}

However if you just want to print if items is present or not (thus not doing other actions) I suggest you to run this code:

public void readIncassiOLG(String filename, List<String []> itemsToCheck) {
    String[] answers = {"", "not "};
    CSVReader reader = new CSVReader(new FileReader(filename), CSV_SEPARATOR);
    List<String []> body = reader.readAll();
    for (String[] item : itemsToCheck) {
        for (String[] bodyItem : body) {
            int present = Arrays.equals(bodyItem, item) ? 0 :  1;
            System.out.println(String.format("Item %spresent", answers[present]));
        }
    }
}

Buon divertimento!

David
  • 2,987
  • 1
  • 29
  • 35