无法走出循环

时间:2013-06-24 08:19:58

标签: java for-loop break if-statement

我一直在使用Apache POI并使用我的代码:

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
    }
}

问题是当我的if语句停止工作时我想离开循环。如果我不使用我的else,一切正常。实际上,如果这个代码无法找到一个字符串,它应该停止,但在我的情况下,else突然与我的if一起工作,在这种情况下只会进行一次迭代。 请告诉我我做错了什么,我只需要提示而不是整体帮助。提前致谢

4 个答案:

答案 0 :(得分:2)

你的问题不清楚,但要探讨:

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
    }
}

答案 1 :(得分:2)

您可以使用带有外部循环标签的break

请查看@ jon-skeet的answer或来自javadoc的此example

答案 2 :(得分:1)

由于第一个循环结束后没有代码,你想break来自return

} else {
    return;
}

虽然标签和break-to-label是有效的语法,但我会避免这种流控制。根据Dijkstra的说法,代码中的一些错误与 goto语句的数量成正比(并且这个中断是它的一个版本)。我只想说:小心一点。如果必须具有类似的流控制,请尝试使用标志来实现它。

答案 3 :(得分:0)

首先,请使用Iterator进行循环(参见参考:http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/

第二:你可能不得不这样做以阻止你的循环。

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
    }
}