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));
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
Re: and statement for a for loop
yup i tried putting i = 0
still having some errors
Index: 14, Size:12
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
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 ..
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
}
}
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 ...
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
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 .....;
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 ...