Problem in loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    Problem in loop

    I am putting this code segment here.......Prob lem is, This loop is running only for i = 0, I dont know what is the problem.....Tha nks in advance...

    Code:
    for(int i = 0; i < v.size(); i++) {
                while(itr.hasNext()) {
                    Map.Entry me = (Map.Entry)itr.next();
                    System.out.println("i::"+i);
                    if((((WorkPackageDetailsVO)v.get(i)).getWorkPackageName()).equals(((WorkPackageSummaryVO)me.getKey()).getWorkPackageName())) {
                        //System.out.println("i:"+i+" TRUE "+me.getKey());
                        String status = ((WorkPackageSummaryVO)me.getKey()).getCurrentDefectStatus();
                        String name = ((WorkPackageSummaryVO)me.getKey()).getWorkPackageName();
                        //System.out.println("----->>>"+status);
                        if(status.equals("On Work Plate"))
                            System.out.println("WorkPackageName: "+name+"  Current Defect Status: "+status+"  Count: "+((WorkPackageSummaryVO)me.getValue()).getCount());
                        else if(status.equals("Fixed"))
                            System.out.println("WorkPackageName: "+name+"  Current Defect Status: "+status+"  Count: "+((WorkPackageSummaryVO)me.getValue()).getCount());
                        else if(status.equals("Removed"))
                            System.out.println("WorkPackageName: "+name+"  Current Defect Status: "+status+"  Count: "+((WorkPackageSummaryVO)me.getValue()).getCount());
                    }
                }
            }
  • itsraghz
    New Member
    • Mar 2007
    • 124

    #2
    It may be because of the number of entries in Iterator "itr". Probably it might have only one entry.

    Can you just check on the number of entries actually present?

    Comment

    • madhoriya22
      Contributor
      • Jul 2007
      • 251

      #3
      Originally posted by itsraghz
      It may be because of the number of entries in Iterator "itr". Probably it might have only one entry.

      Can you just check on the number of entries actually present?

      I check it. Iterator has more than one element. But at the change of i from 0 to 1 while loop is not iterating again......I think at the change of i while loop should start from the begining.....I dont know why its happenin..

      Comment

      • praveen2gupta
        New Member
        • May 2007
        • 200

        #4
        chech value of the v.size(), either this is not working or returned value is 1.
        or
        while(itr.hasNe xt()) check the return type of the itr.hasNext(), while loop will work till condition is true. This condition must return the boolean value.

        Comment

        • madhoriya22
          Contributor
          • Jul 2007
          • 251

          #5
          Originally posted by praveen2gupta
          chech value of the v.size(), either this is not working or returned value is 1.
          or
          while(itr.hasNe xt()) check the return type of the itr.hasNext(), while loop will work till condition is true. This condition must return the boolean value.

          I check itr.hasNext().. .for i = 1,2,3 it is returning false......but at the change of i the while loop should start from the begining....any suggestion??

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by madhoriya22
            I check itr.hasNext().. .for i = 1,2,3 it is returning false......but at the change of i the while loop should start from the begining....any suggestion??
            Is there any relationship between iter and v?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by r035198x
              Is there any relationship between iter and v?
              i.e Why are you using both a for and a while to do the looping?

              Comment

              • madhoriya22
                Contributor
                • Jul 2007
                • 251

                #8
                Originally posted by r035198x
                i.e Why are you using both a for and a while to do the looping?
                Actually v is a vector it contains workpackageName s.......So I am comparing name in vector and name in iterator......t o get the no of counts corresponding to status in that workpackage.... .plz tell me if there is any other way to do it......

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by madhoriya22
                  Actually v is a vector it contains workpackageName s.......So I am comparing name in vector and name in iterator......t o get the no of counts corresponding to status in that workpackage.... .plz tell me if there is any other way to do it......
                  change your loops and put the for inside the while. Once the loop runs for the first i, iter.hasNext is no longer true for subsequent values of i.

                  Comment

                  • praveen2gupta
                    New Member
                    • May 2007
                    • 200

                    #10
                    Originally posted by madhoriya22
                    I check itr.hasNext().. .for i = 1,2,3 it is returning false......but at the change of i the while loop should start from the begining....any suggestion??

                    Hi
                    Your reply
                    itr.hasNext().. .for i = 1,2,3 it is returning false......

                    while condition is returning a false value so it will not enter in the loop. It should return a true value.

                    itr.hasNext() is not working or the itr does not have any data. Check itr.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by praveen2gupta
                      Hi
                      Your reply
                      itr.hasNext().. .for i = 1,2,3 it is returning false......

                      while condition is returning a false value so it will enter in the loop. It should return a true value.

                      itr.hasNext() is not working or the itr does not have any data. Check itr.
                      The iterator could have values but it runs through all of them with i = 0 because of the next being called.

                      Comment

                      • madhoriya22
                        Contributor
                        • Jul 2007
                        • 251

                        #12
                        Originally posted by r035198x
                        The iterator could have values but it runs through all of them with i = 0 because of the next being called.

                        If I initialize iterator after for loop and before while loop then, will it help me?........

                        I have already put for loop in while loop........but by doing that output was not according to requirement.... ...

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by madhoriya22
                          If I initialize iterator after for loop and before while loop then, will it help me?........

                          I have already put for loop in while loop........but by doing that output was not according to requirement.... ...
                          Do you understand the logic of the next and hasNext methods?
                          See here
                          [CODE=java]for (int i = 0; i < v.size(); i++) {
                          //i = 0;
                          while (iter.hasNext() ) {
                          ....iter.next() ;
                          }
                          //while is done => iter.hasNext() is false
                          //now when the for index changes to 1, the while loop is not entered
                          //any more because iter.hasNext() is false;
                          }[/CODE]

                          Comment

                          • madhoriya22
                            Contributor
                            • Jul 2007
                            • 251

                            #14
                            Originally posted by r035198x
                            Do you understand the logic of the next and hasNext methods?
                            See here
                            [CODE=java]for (int i = 0; i < v.size(); i++) {
                            //i = 0;
                            while (iter.hasNext() ) {
                            ....iter.next() ;
                            }
                            //while is done => iter.hasNext() is false
                            //now when the for index changes to 1, the while loop is not entered
                            //any more because iter.hasNext() is false;
                            }[/CODE]
                            My last reply was half........her e v is Vector and iterator is for HashMap

                            Anyway....got your point......Now its done.....Thanks a lot.....

                            Comment

                            • joloms38
                              New Member
                              • Jul 2007
                              • 1

                              #15
                              hi. there..

                              how to use while loop in java using an exit?....
                              my problem is, i can not exit the program using the loop given to me...
                              please help me.....

                              Comment

                              Working...