2013-06-24 58 views
1

我一直在使用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。實際上,如果這段代碼無法找到一個字符串,它應該停止,但在我的情況下,其他突然與我的工作,如果只有一個迭代將發生在這種情況下。 請告訴我我做錯了什麼,請我只需要提示,而不是整體幫助。在此先感謝

+2

「*的問題是,我想離開循環時,我的‘if’語句停止工作,*!」 - 如何'如果'語句停止工作?我不明白你的意思。並請縮進你的代碼,以便我們(和你)更容易幫助你。 – Maroun

+0

我只需要擺脫我的循環,當我的控制來到其他語句!它很簡單,但我的代碼沒有這樣做! – user2496503

+0

在reindent之後,如果你需要另一個else if:(cell.getCellType()== Cell.CELL_TYPE_STRING){'。順便說一句,休息只會打破內循環而不是外循環。如果你想突破 – TecHunter

回答

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

不錯,但我會補充說,不建議在'for'循環中使用'break'語句,最好使用'while 'loop instead – BackSlash

+0

@BackSlash true,但這不是問題:)我討厭休息並繼續:P – TecHunter

+1

這不是問題,但總是接受一個旁註,程序員需要知道他們在哪裏做錯了什麼;) – BackSlash

0

首先請使用迭代器爲您的循環(見參考文獻: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 
    } 
} 
+0

你的代碼不能編譯。 'flag'類型不存在,也許你想寫'boolean'? – BackSlash

+0

我現在糾正了,謝謝。 –

1

既然有你,只是returnbreak第一循環結束後,沒有代碼。

} else { 
    return; 
} 

雖然標籤和中斷標籤是有效的語法,我會避免這樣的流量控制。根據Dijkstra的說法,代碼中的一些錯誤與goto語句的數量成正比(並且該中斷是它的一個版本)。我只想說:小心一點。如果您必須具有類似的流量控制,請嘗試使用標誌來實現它。

2

您可以使用break以及外部循環的標籤。

請大家一看@喬恩 - 雙向飛碟的answer或者這example從javadoc中