Home > Net >  How to store into an array, in Java, the rows of another array that contain a word
How to store into an array, in Java, the rows of another array that contain a word

Time:07-02

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

CodePudding user response:

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;
            }
        }
    }
  • Related