gpt4 book ai didi

java - 无法摆脱循环

转载 作者:行者123 更新时间:2023-11-29 07:56:44 25 4
gpt4 key购买 nike

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

最佳答案

你的问题不清楚,但探索一下:

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

关于java - 无法摆脱循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17270899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com