【问题标题】:unable to get out the Loop无法摆脱循环
【发布时间】:2013-06-20 16:33:13
【问题描述】:

我一直在使用 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 一起工作,在这种情况下只会发生一次迭代。 请告诉我我做错了什么,我只需要提示而不是整体帮助。提前致谢

【问题讨论】:

  • "问题是当我的 "if" 语句停止工作时我想退出循环!" - if 语句如何停止工作?我不明白您的意思。请缩进您的代码,以便我们(和您)更容易为您提供帮助。
  • 当我控制 else 语句时,我只需要退出我的循环!很简单,但我的代码没有这样做!
  • 在 reindent 之后也许你需要另一个 else 作为第一个 if :if (cell.getCellType() == Cell.CELL_TYPE_STRING) {。顺便说一句,break 只会破坏内循环而不是外循环。如果您想跳出第一个循环,则需要标记它
  • 使用调试器。你可能不满足外部if:if (cell.getCellType() == Cell.CELL_TYPE_STRING)所以你会进行下一次迭代。
  • @user2496503 你想停止两个循环中的哪一个?内部的还是外部的?您的问题完全不清楚,请尝试将其编辑为能够真正解释您的问题的内容

标签: java for-loop break if-statement


【解决方案1】:

您的问题不清楚,但请探索:

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

【讨论】:

  • 很好,但我要补充一点,不建议在for 循环中使用break 语句,而是使用while 循环会更好
  • @BackSlash true 但这不是问题:) 我讨厌休息并继续:P
  • 这不是问题,但总是可以接受旁注,程序员需要知道他们在哪里做错了;)
  • @BackSlash 我完全同意别担心 :)
【解决方案2】:

您可以将break 与外部循环的标签一起使用。

请查看 @jon-skeet 的 answer 或 javadoc 中的 example

【讨论】:

    【解决方案3】:

    由于在第一个循环结束后没有代码你想从break 中,只需return

    } else {
        return;
    }
    

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

    【讨论】:

      【解决方案4】:

      首先请为您的循环使用迭代器(请参阅参考: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
          }
      }
      

      【讨论】:

      • 您的代码无法编译。 flag 类型不存在,也许你想写boolean
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 2014-09-05
      • 2018-01-14
      • 1970-01-01
      相关资源
      最近更新 更多