0

forループ内でarraylistの値を比較しているメソッドがあります。Loop 2loop1 に対して 1 つの条件が true の場合、(以下のコードを参照) を実行したくありません。

さて、ハッピングとは、いくつかの値loop 1はstatisfyであり、他のいくつ かの値は満足しloop 2ています.だから、これのせいで、私はdbが間違ったデータを入力しています.

そのような方法でコードを変更したいと思います。配列リストの値のいずれかが満足しているloop 1場合、コンパイラは から戻る必要がありreturn ERRORます。その後、コードを実行しないでください。

ループチェック条件はif(quant>Integer.parseInt(book.getQuantity()))

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

2 に答える 2

1

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; 
      }
于 2012-11-26T20:12:51.693 に答える
0

これを実現するには、break ステートメントを使用する必要があります。代わりに、ローカルのブール変数を作成し、それを使用してループ 2 に入らないようにすることもできます。

    if(quant>Integer.parseInt(book.getQuantity()))
          { flag=false;
}

if(flag){
//loop 2
}
于 2012-11-26T20:15:11.470 に答える