user2496503
user2496503

Reputation: 927

unable to get out the Loop

I have been working with Apache POI and using my this code:

private void remove() {
    HSSFWorkbook wb = null;
    HSSFSheet sheet = null;
    try {
        FileInputStream is = new FileInputStream("C:/juni.xls");
        wb = new HSSFWorkbook(is);
        sheet = wb.getSheetAt(0);
        for (Row row : sheet) {
            for (Cell cell : row) {
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    if (cell.getRichStringCellValue().getString().trim().contains("POST")) {
                        row_count_post_1 = row.getRowNum();
                        DashBoard.txt.setText("Processed the STRING \"POST\" on Row#" + row_count_post_1);
                        HSSFRow removingRow = sheet.getRow(row_count_post_1);
                        if (removingRow != null) {
                            sheet.removeRow(removingRow);
                        }
                        int lastIndex = sheet.getLastRowNum();
                        sheet.shiftRows(row_count_post_1 + 1, lastIndex, -1);
                    } else {
                        break;
                    }////////////want to break from here;
                }
            }
        }
    } catch (Exception e) {
        //do catch for no exception
    }
}

The problem is that I want to get out of the loop when my if statement stops working. All is working fine if i don't use my else. Actually if a this code fails to locate a string it should stop but in my case the else suddenly works with my if and only one iteration will take place in this case. Please tell me what I'm doing wrong please i just need the hints not the overall help. Thanks in advance

Upvotes: 1

Views: 171

Answers (4)

darijan
darijan

Reputation: 9785

Since there is no code after the finishing of the first loop you want to break from, just return.

} else {
    return;
}

Although labels and break-to-label is valid syntax, I would refrain from such flow control. According to Dijkstra, a number of bugs in the code is proportional to the number of goto statements (and this break is a version of it). I just want to say: be careful with it. If you must have similar flow control, try to implement it using flags.

Upvotes: 1

Shreyas Dave
Shreyas Dave

Reputation: 3855

First of all please use Iterator for your loops (See Ref : http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/)

Second: You may have to do like this to stop your loop.

private void remove() {
    HSSFWorkbook wb = null;
    HSSFSheet sheet = null;
    try {
        FileInputStream is = new FileInputStream("C:/juni.xls");
        wb = new HSSFWorkbook(is);
        sheet = wb.getSheetAt(0);
        for (Row row : sheet) {
            boolean stop = false;
            for (Cell cell : row) {
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    if (cell.getRichStringCellValue().getString().trim().contains("POST")) {
                        row_count_post_1 = row.getRowNum();
                        DashBoard.txt.setText("Processed the STRING \"POST\" on Row#" + row_count_post_1);
                        HSSFRow removingRow = sheet.getRow(row_count_post_1);
                        if (removingRow != null) {
                            sheet.removeRow(removingRow);
                        }
                        int lastIndex = sheet.getLastRowNum();
                        sheet.shiftRows(row_count_post_1 + 1, lastIndex, -1);
                    } else {
                        stop = true;
                        break;
                    }
                }
                if (stop) {
                    break;
                }
            }
        }
    } catch (Exception e) {
        //do catch for no exception
    }
}

Upvotes: 0

M Abbas
M Abbas

Reputation: 6479

You can use break with a label for the outer loop.

Please take a look on @jon-skeet's answer Or this example from the javadoc

Upvotes: 2

TecHunter
TecHunter

Reputation: 6141

You question is unclear but explore that :

private void remove(){
    HSSFWorkbook wb = null;
    HSSFSheet sheet=null;

    try {

        FileInputStream is = new FileInputStream("C:/juni.xls");

        wb = new HSSFWorkbook(is);
        sheet = wb.getSheetAt(0);
        //Tagging the loops
        OuterLoop : for (Row row: sheet) {
            InnerLoop : for (Cell cell: row) {

                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {

                    if (cell.getRichStringCellValue().getString().trim().contains("POST")) {

                        row_count_post_1 = row.getRowNum();

                        DashBoard.txt.setText("Processed the STRING \"POST\" on Row#" + row_count_post_1);
                        HSSFRow removingRow = sheet.getRow(row_count_post_1);

                        if (removingRow != null) {
                            sheet.removeRow(removingRow);
                        }
                        int lastIndex = sheet.getLastRowNum();
                        sheet.shiftRows(row_count_post_1 + 1, lastIndex, -1);
                    } else {
                        break InnerLoop; // OR break OuterLoop;
                    } ////////////want to break from here;
                }
            }
        }
    } catch (Exception e) {
        //do catch for no exception
    }
}

Upvotes: 2

Related Questions