1

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

BackSlash
  • 21,927
  • 22
  • 96
  • 136
user2496503
  • 907
  • 5
  • 13
  • 21
  • 2
    "*The problem is that I want to get out of the loop when my "if" statement stops working!*" - How `if` statements stop working? I don't understand what you mean.. And please indent your code so it'll be easier for us (and you) to help you. – Maroun Jun 24 '13 at 08:21
  • i just need to get out of my loop when my control comes to else statement! it simple but my code is failing doing so! – user2496503 Jun 24 '13 at 08:25
  • after reindent maybe you need another else for the first if : `if (cell.getCellType() == Cell.CELL_TYPE_STRING) {`. BTW, the break will only break the inner loop not the outer loop. you will need to tag the first loop if you want to break out of that – TecHunter Jun 24 '13 at 08:25
  • **Use a debugger**. You're probably not satisfying the outer `if`: `if (cell.getCellType() == Cell.CELL_TYPE_STRING)` So you'll proceed to the next iteration. – Maroun Jun 24 '13 at 08:26
  • @user2496503 Which of the two loops do you want to stop? the inner one or the outer one? Your question is totally unclear, try editing it to something that really explains your problem – BackSlash Jun 24 '13 at 08:26

4 Answers4

2

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
    }
}
TecHunter
  • 6,091
  • 2
  • 30
  • 47
2

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

Community
  • 1
  • 1
M. Abbas
  • 6,409
  • 4
  • 33
  • 46
1

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.

darijan
  • 9,725
  • 25
  • 38
0

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
    }
}
Shreyas Dave
  • 3,815
  • 3
  • 28
  • 57