[英]how to avoid code from execution which are outside of the loop
I have a method, where i am comparing arraylist values inside the for loop. 我有一种方法,其中我比较for循环内的arraylist值。 I do not want to execute the Loop 2
(see below code) if any single condition is true for loop1. 如果loop1有任何单个条件为真,我就不想执行Loop 2
(请参见下面的代码)。
Now what happing is, for some values loop 1
is statisfy and for some other values loop 2
satisfy.So beacuse of this i am db is populating with wrong data. 现在的问题是,对于某些值, loop 1
可以统计,而对于某些其他值, loop 2
可以满足。由于这个原因,我在db中填充了错误的数据。
I want to modify my code in such a way that. 我想以这种方式修改我的代码。 if any of the array list values are stisfying the loop 1
then compiler should return from return ERROR
. 如果任何数组列表值满足loop 1
则编译器应从return ERROR
。 It should not execte the code after that. 在那之后它不应该执行代码。
Loop checking condition is if(quant>Integer.parseInt(book.getQuantity()))
循环检查条件为if(quant>Integer.parseInt(book.getQuantity()))
Action.java. Action.java。
public String execute()
{
if(id.length == quantity.length)
{
for (int i = 0; i < id.length; ++i)
{
book = dao.listbookdetailsByBId(id[i]);
Double dq=new Double(quantity[i]);
int quant=dq.intValue();
if(quant>Integer.parseInt(book.getQuantity()))
{
//Loop 1 , this is executing if any of the quant is greater then book.getQuantity()..
//i want to stop executing LOOP2 ,if any of the value of quant is greater then book.getQuantity().
addActionError("You have entered an invalid quantity for the Book Title- ''"+book.getBookTitile()+"''.");
return ERROR;
}
/* Loop2 starts here
* Loop 2 , this is executing if any of the quant is lesser ..
* The below code should execute only if compiler does not reach to the loop1 */
// Some DAO code goes here
}
}
return SUCCESS;
}
Just use a break statement 只需使用break语句
if(quant>Integer.parseInt(book.getQuantity()))
{
//Loop 1 , this is executing if any of the quant is greater then book.getQuantity()..
//i want to stop executing LOOP2 ,if any of the value of quant is greater then book.getQuantity().
addActionError("You have entered an invalid quantity for the Book Title- ''"+book.getBookTitile()+"''.");
break;
}
You should use a break statement to achieve this, alternatively you can create a local boolean variable and use it to avoid going in loop 2. 您应该使用break语句来实现此目的,或者可以创建一个局部布尔变量,并使用它避免进入循环2。
if(quant>Integer.parseInt(book.getQuantity()))
{ flag=false;
}
if(flag){
//loop 2
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.