cancel
Showing results for 
Search instead for 
Did you mean: 

compare values of two drop downs

Former Member
0 Kudos

Dear SDN,

Pls help me in doing the code for the following scenario,

I have two dynamically populated drop downs with different sizes

now I want to loop through the firstdropdown and compare each value with the second drop down values,

if the first dropdown value does not exists in the second dropdown then it must return a message saying that the value does not exist in the second drop down..

I have tried using the followin code

for(int a =o;a<firstdropdown.size;a++){

string abc = firstdropdown.getelement(a).value

for(int b =o;a<seconddropdown.size;b++){

string xyz= seconddropdown.getelement(b).value

if(!abc .equals(xyz)

{

message abc is not found in the second dropdown

}

}

}

as it is a nested loop this code is returing some non relevant values

can some body pls help me in getting the correct code for comparing

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Here i guess you are making some mistake...

if(!abc .equals(xyz)
{

message abc is not found in the second dropdown

}

Modify it as :

int found =0;
for(int a =o;a<firstdropdown.size;a++){
string abc = firstdropdown.getelement(a).value

for(int b =o;a<seconddropdown.size;b++){

string xyz= seconddropdown.getelement(b).value

if(abc.equals(xyz))
{
found = 1;
Break from outer loop;
}


}

}// end of outer loop;

if(found == 0)
{
// message string not found
}

Edited by: Saurabh Agarwal on Sep 3, 2010 5:39 AM

Former Member
0 Kudos

Hi

Thanks for your reply .. unfortunately the code is not working ..

my first dropdown values :

A1

A2

A3

A4

A5

my Second dropdown values :

A1

A2

A4

A5

A3 values is not there in the second drop down .. I want to print this A3 only, can somebody pls guide according to this .. pls

Thanks

Former Member
0 Kudos

OK...

just modify it little bit more :

Replace

}// end of outer loop;
 
if(found == 0)
{
// message string not found
}

with

if(found == 0)
{
// message string not found
}
}// end of outer loop;

AND

if(abc.equals(xyz))
{
found = 1;
Break from outer loop;
}

with

if(abc.equals(xyz))
{
found = 1;
Break from inner loop;
}

Edited by: Saurabh Agarwal on Sep 3, 2010 11:48 AM

Former Member
0 Kudos

Hi Saurabh,

here is my exact code

int found =0;

for(int c = 0; c<wdContext.nodefirst().size();c++){

String cNum = wdContext.nodefirst.getnodefirstAt(c).getId().toString();

for(int a=0; a<mV.getValuesCount(); a++){

String mId = mV.getValue(a).toString();

if(mId.equals(cNum)==true){

found =1;

Break ;

}

}else

msgMgr.reportSuccess("value"+cNum);

}

}

when I am printing I am getting the following vals instead of A3

value A2

value A3

value A3

value A3

value A3

value A4

value A4

value A5

value A5

value A5

I hope it is clear

Thanks

Former Member
0 Kudos
if(mId.equals(cNum)==true){
found =1;
Break ;
}
}else
msgMgr.reportSuccess("value"+cNum); 
}// inner loop
}// outer loop

Remove else

AND

just put below condition between end peranthesis of loops.

if(mId.equals(cNum)==true){
found =1;
Break ;
}
}// inner loop
if(found==0)
{
msgMgr.reportSuccess("value"+cNum); 
}
}// outer loop

Former Member
0 Kudos

Hi Saurabh,

if(found==0)

{

msgMgr.reportSuccess("value"+cNum);

}

no message returned.. I think found is not getting 0

if(mId.equals(cNum)==true){

found =1;

break;

}

this statement is exactly giving me the values which are equal. and it is not giving the ones which are not equal, as it is a nested loop it is getting too many values like this ..

value A2

value A3

value A3

value A3

value A3

value A4

value A4

value A5

value A5

value A5

Former Member
0 Kudos

Ohhhhh !

We have to reset this found variable in beginning of inner loop !

Set found = 0;

just abobe your inner for loop.

String cNum = wdContext.nodefirst.getnodefirstAt(c).getId().toString();
found = 0;
for(int a=0; a<mV.getValuesCount(); a++){

Former Member
0 Kudos

thanx saurabh... it worked..

Former Member
0 Kudos

Welcome !

But you must optimize the solution....

If both dropdowns have M and N values respectively,

This in worse case (where no values are found).

Your loops will execute M*N times !

Answers (0)