matching the values from an array containing values

94 views Asked by At

I have an array containing the following values

final String[] headers = { "abc", "def", "rty", "yui"  };

now i am iterating over the excel sheet cells by using apache poi as shown below

 for (Row row : firstSheet) {

            for (Cell cell : row) {
              if ("abc".equals(cell.getStringCellValue())) {

now as shown below the value "abc" is hardcoded that is i want to customize it in such a way that when cell value is read then it should be matched from witih in the array i want to remove this hard coding so in other words the cell value is read which will always be a string and then that same value is searched with in the array itself please advise how to customise this

3

There are 3 answers

2
Naman Gala On

You can create List instead of Array and then use contains method in if clause.

0
TheCodingFrog On

I would strongly recommend to use a Set instead of an Array. Here's the code:

public static void main(String[] args) {
        final Set<String> headers = new HashSet<String>();
        headers.add("abc");
        headers.add("def");
        headers.add("rty");
        headers.add("yui");

        for (Row row : firstSheet) {

            for (Cell cell : row) {
                if(headers.contains(cell.getStringCellValue())) {
                    // matching 
                } else {
                    // not matching
                }
            }
        }
    }
0
Dharamendra Prajapati On

Use the below snippet to replace the hardcode values.

List<String> listHeader=Arrays.asList(header);
for (Row row : firstSheet) {
    for (Cell cell : row) {
        if(listHeader.contains(cell.getStringCellValue()) {

        }
    }
}