Search code examples
javacsvopencsv

How to check for corresponding words in a string to a CSV file?


CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
CSVReader csvReader = new CSVReaderBuilder(filereader).withCSVParser(parser).withSkipLines(1).build();
List<String[]> allData = csvReader.readAll();

String studentName = "Alice, Bob";
String[] words = studentName.split(",");

List<String> names = new ArrayList<>();

for(String[]row:allData){ // goes through the csv file
        for(String word:words){ // goes through String names
        if(row[3].contains(word)){
        names.add(row[4]);
        }
    }
}

The issue is that the for-loop only goes through the CSV file to check for "Alice", and doesn't check "Bob", and therefore Bob does not get added to the list, how would I solve that?

row[3] = names

row[4] = school

@xerx593 spotted the mistake, this sovled it:

String[] words = studentName.split(", ");

Solution

  • Was a spelling error.

    @xerx593 spotted the mistake, this sovled it:

    String[] words = studentName.split(", ");