CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2005
    Posts
    40

    and statement for a for loop

    hi there , how do i include a and condition for a for loop

    i am wanting to extract out "uyt" and "HostingClientMNC"
    but it returns me "java.lang.IndexOutOfBoundsException: Index: 13, Size: 12" error

    Code:
    arr.add("ns1:ClassificationNode");
    arr.add("uyt");
    arr.add("lid=urn:oasis:names:tc:ebxml-regrep:SubjectRole:uyt");
    arr.add("objectType=urn:oasis:names:tc:ebxml-regrep:ObjectType:RegistryObject:ClassificationNode");
    arr.add("status=urn:oasis:names:tc:ebxml-regrep:StatusType:Submitted");
    arr.add("id=urn:oasis:names:tc:ebxml-regrep:SubjectRole:uyt");
    arr.add("ns1:ClassificationNode");
    arr.add("HostingClientMNC");
    arr.add("lid=urn:oasis:names:tc:ebxml-regrep:SubjectRole:HostingClientMNC");
    arr.add("objectType=urn:oasis:names:tc:ebxml-regrep:ObjectType:RegistryObject:ClassificationNode");
    arr.add("status=urn:oasis:names:tc:ebxml-regrep:StatusType:Submitted");
    arr.add("id=urn:oasis:names:tc:ebxml-regrep:SubjectRole:HostingClientMNC");
    
    for ( int i = 1; i <= arr.size();)
    {
    	i++;
    	i++;
    	i++;
    	i++;	
    	i++;
    	i++;
    	i++;
                    i++;
    
    System.out.println(arr.get(i));

  2. #2
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: and statement for a for loop

    Hi,

    The reason you are getting the exception is that you are initialising the control variable of your for loop - i to 1, you should init to 0. ie:

    Code:
    for ( int i = 0; i <= arr.size();)
    Remember counting in arrays starts at 0 not 1.

    Byron

  3. #3
    Join Date
    Feb 2005
    Posts
    40

    Re: and statement for a for loop

    yup i tried putting i = 0

    still having some errors

    Index: 14, Size:12

  4. #4
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: and statement for a for loop

    Hi,


    Sorry, just woke up, wasn't concerntrating properly. Because you don't actually increment variable i in your for loop statement. ie:
    Code:
    for ( int i = 0; i <= arr.size();  increment would be here normally  )
    This creates an infinite loop, BUT you are incrementing i inside the loop. Now what is happening is that you have 12 elements in your array and only increment i 8 times, so the for loop executes again because i is still smaller than or equal to arr.size() yet, so by the time you get to
    Code:
    System.out.println(arr.get(i));
    the second time, i is more than all available indexes in your array, does this make sense? This is actually really bad use of a for loop.

    Why not loop through the array as normal and test for "uyt" and "HostingClientMNC" with the String equals() method on each iteration of the loop.

    Byron

  5. #5
    Join Date
    Feb 2005
    Posts
    40

    Re: and statement for a for loop

    hi there , thx for the reply .. but those 2 values does is not always there ... they may change ..

  6. #6
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: and statement for a for loop

    Yes, so if you test for them and they aren't there, then don't take any action ie:


    Code:
    for(int i =0; i < arr.size(); i++){
    	if(((String)arr.get(i)).equals("uyt")){
    		// Do something
    	}else if(((String)arr.get(i)).equals("HostingClientMNC")){
    		// Do Something
    	}
    }

  7. #7
    Join Date
    Feb 2005
    Posts
    40

    Re: and statement for a for loop

    what i meant was that i would not know what the new data thats going to be added in to do the .equals method ...

  8. #8
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: and statement for a for loop

    OK, then test for the specific index, although that makes no sense, if you know the index of the item you are looking for why bother looping over the array?

    Byron

  9. #9
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: and statement for a for loop

    So you want to do a linear search through the ArrayList to search for 2 String values. There's no need to code an explicit loop to search through the List since the code is already given to you in the form of indexOf():

    Code:
    int uytPos = arr.indexOf("uyt");
    int HostClientMNCPos = arr.indexOf("HostingClientMNC");
    
    if (uytPos > 0) System.out.println(arr.get(uytPos));
    else .....;
    
    if (HostClientMNCPos > 0) System.out.println(arr.get(HostClientMNCPos));
    else .....;
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  10. #10
    Join Date
    Feb 2005
    Posts
    40

    Re: and statement for a for loop

    thx for all the reply .. my problem is solved ...

    btw .. how can this problem be solved ... whenever i click on the submit button , the value of the combo box already revert back to the first value even if before submitting i clicked the last value ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured