0

I have an array that contains all the data from an excel table in the same order. I want to save in an array the rows that contain the word "execute" in column 4 of my array. Do you think you can guide me on this? Here I leave you a reference and the method built so far.

My array looks like this and its name is 'dataTable':

MONTH SUMMARY CARD SPRINT ACTION CLOSE
april one fix 1 execute yes
may two script 2 n/a no
june three fix 3 execute yes

The other array should look like this and its name is 'execute':

MONTH SUMMARY CARD SPRINT ACTION CLOSE
april one fix 1 execute yes
june three fix 3 execute yes

My code is:

public static int numToExecute() {
    executions = 0;
    for (String[] row : dataTable) {
        if (header == false) {
            for (String field : row) {
                if (field.equals("execute")) {
                    executions++;
                }
            }
        }
        header = false;
    }
    execute = new String[executions][dataTable[0].length];
    //System.out.println(executions);
    return executions;
}

//Method to transfer rows from array 'dataTable' to array 'execute'
public static String listToExecute() {
    //The dataTable array already contains all the data of the excel table, including the header
    execute = new String[numToExecute()][dataTable[0].length];
    for (int i = 1; i < dataTable.length; i++) {//I initialize i to 1 because I don't want to extract the header from the table
        System.out.println("***************EXECUTION*******************" + dataTable.length);

        for (int j = 0; j < dataTable[0].length; j++) {
            if (dataTable[i][4].contains("execute")) {//Here I filter the rows that contain "execute" in column 4
                execute[i - 1][j] = dataTable[i][j];//<---This is where it fails: I put 'i-1' because I want the first row to be filtered 
                //be stored at execute[0][0] 
                System.out.println(execute[i - 1][j]);//It should print every value stored at the 'execute' array
            }
            continue;
        }
    }
    return "";
}

The code that I use to read and save the Excel table to the dataTable array is:

public static String readExcel(String fileName, String sheetName) {

    // Create a file input stream to read Excel workbook and worksheet
    xlFile = new File(".\\" + fileName);
    fis = new FileInputStream(xlFile);
    xlWB = new XSSFWorkbook(fis);
    xlSheet = xlWB.getSheet(sheetName);

    // Get the number of rows and columns
    int numRows = xlSheet.getLastRowNum() + 1;
    int numCols = xlSheet.getRow(0).getLastCellNum();

    // Create double array data table - rows x cols
    // We will return this data table
    dataTable = new String[numRows][numCols];

    // For each row, create a HSSFRow, then iterate through the "columns"
    // For each "column" create an HSSFCell to grab the value at the specified cell
    // (i,j)
    for (int i = 1; i < numRows; i++) {
        XSSFRow xlRow = xlSheet.getRow(i);
        //System.out.println("**********************************************");
        for (int j = 0; j < numCols; j++) {                                     
            XSSFCell cell = xlRow.getCell(j);                       
            dataTable[i][j] = cell.toString();  
            //System.out.println(dataTable[i][j]);                              
        }
    }
}

And the result that I get is:

enter image description here

result

result

Adrian
  • 13
  • 3
  • 1
    Try `if("execute".equals(field))` Alternatively, try `if(field != null && field.equals("execute")) {` Also refer to [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Abra Jul 01 '22 at 02:39
  • Haven't read the code but should be as trivial as `Arrays.stream(dataTable).filter(row -> Objects.equals(row[4], "execute")).toArray(String[][]::new);` – plalx Jul 01 '22 at 02:55
  • @Abra I have posted the result of your answer in the last image – Adrian Jul 01 '22 at 03:09

1 Answers1

0

You can use this -

    public static void main(String[] args) {
    String[][] dataTable = {{"MONTH", "SUMMARY", "CARD", "SPRINT", "ACTION", "CLOSE"},
            {"april", "one", "fix", "1", "execute", "yes"},
            {"may", "two", "script", "2", null, "no"},
            {"june", "three", "fix", "3", "execute", "yes"}};
    List<String[]> outList = new ArrayList<>();
    int i = 0;
    for (String[] row : dataTable) {
        if (i == 0 || null != row[4] && row[4].contains("execute")) {
            outList.add(row);
        }
        i++;
    }

    for (String[] aa : outList) {
        System.out.println(Arrays.toString(aa));
    }

    String[][] arrayFormat = outList.toArray(new String[0][0]);
    for (String[] aa : arrayFormat) {
        System.out.println(Arrays.toString(aa));
    }

}

Also while populating datatable you are starting first for loop from 1 instead of 0 so, I think you missing headers in your data table, also you are doing cell.toString() with any null check so you might get NullPointer, below is the enhanced for loop -

        for (int i = 0; i < numRows; i++) {
        XSSFRow xlRow = xlSheet.getRow(i);
        for (int j = 0; j < numCols; j++) {
            XSSFCell cell = xlRow.getCell(j);
            if (null != cell) {
                dataTable[i][j] = cell.toString();
            } else {
                dataTable[i][j] = null;
            }
        }
    }
  • Hello @tomar, I have applied your solution, but I get rows null and the first row of data is repeated twice. You can see the result in the last image – Adrian Jul 01 '22 at 14:10
  • can you please show me, what data you are trying to process, I think your data is not correct as per your explanation in your original post. –  Jul 01 '22 at 14:15
  • Yes, I have put the code right now in the post. I think as you say, that maybe my code to build dataTable is wrong because If I run all your solution, this run fine, it's not the case for my dataTable. – Adrian Jul 01 '22 at 14:58
  • I have edited my response as I can see issues in your code for population of data table, please use this and let me know then. –  Jul 01 '22 at 15:57
  • Oh, you are right: it's necessary to do the validation for null cells. Thank you for your answer @tomar! – Adrian Jul 01 '22 at 16:47