简体   繁体   中英

Check if String array contains word in another String Array

Having trouble figuring out how to check if a word from one String Array is in another String Array. This is what I have so far:

        FileInputStream fis = new FileInputStream("TranHistory.csv");
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        CSVReader reader = new CSVReader(isr);

        String[] groceries = new String[]{"albertsons", "costco"};

        for (String[] cols; (cols = reader.readNext()) != null;) {
            if(cols[4].toLowerCase().contains(groceries)){
                System.out.print(cols[4]);
            }

        }

The above code is currently giving me an error because .contains() cannot be applied to String Array. This only works if I change the if statement to this:

        if(cols[4].toLowerCase().contains("albertsons")){
                System.out.print(cols[4]);
        }

My issue is that String[] groceries is going to have many grocery stores so I think comparing String[] col to String[] groceries is the most efficient way of doing this I'm just having trouble implementing it.

SOLUTION:

I figured it out... you have to do a nested for loop. This is what I did:

String[] groceries = {"albertsons", "costco"};

for (String[] cols; (cols = reader.readNext()) != null;) {
      for (int i = 0; i < groceries.length; i++){

          if(cols[4].toLowerCase().contains(groceries[i]))
             {
                 System.out.print(cols[4]);
                 }

           }
      }

I recommend creating a Set<String> containing all of the groceries that you plan to have:

Set<String> groceries = Set.of("albertsons", "costco");

for (String[] cols; (cols = reader.readNext()) != null;) {
    if (groceries.contains(cols[4].toLowerCase()){
        System.out.print(cols[4]);
    }
}

Searching in a Set will not take linear time as it would if you had used an array.

As YCF_L and I explained in the comments below, you can initialize the Set in Java 8 with:

Set<String> groceries = new HashSet<>(Arrays.asList("albertsons", "costco"));

I would typically do this via Hashset as it searches the elements in constant time rather than linear time. So you can use this code for searching. I am assuming that you want the whole original array from the file to be printed when found.

FileInputStream fis = new FileInputStream("TranHistory.csv");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
CSVReader reader = new CSVReader(isr);

String[] groceries = new String[]{"albertsons", "costco"};
Set<String> grocerySet = Arrays.stream(groceries).collect(Collectors.toSet());
System.out.println(grocerySet);
for (String[] cols; (cols = reader.readNext()) != null;) {
    Set<String> smallGrocerySet = Arrays.stream(cols).collect(Collectors.toSet());
    if(grocerySet.containsAll(smallGrocerySet)){
        System.out.println(Arrays.toString(cols));
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM