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
ifstatements 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.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 thatif:if (cell.getCellType() == Cell.CELL_TYPE_STRING)So you'll proceed to the next iteration.