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_member214651
Active Contributor
0 Kudos

Hi,

Try this piece of code:

boolean valueFound;
for(int i =0; i < firstdropdown.size; i++)
{
      valueFound = false;
      string firstValue = firstdropdown.getelement(i).value();
      for(int j =0; j < seconddropdown.size; j++)
      {
            string secondValue = seconddropdown.getelement(j).value
            if((firstValue.equals(secondValue))
            {
                 valueFound = true;
                 break;
            }
      }
      if(!valueFound)
      {
             //Report the message
      }
}

Hope this helps you

Regards,

Poojith MV

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

Hi when I am doing the comparions as the if((firstValue.equals(secondValue)).. I am getting the correct values,

but I need the value which is notequals to .. I am putting an else statement which has resulted in the following instead of A3 value ...

value A2

value A3

value A3

value A3

value A3

value A4

value A4

value A5

value A5

value A5

Former Member
0 Kudos

Hi,

Don't put any else statement. Just modify the code as below:


boolean valueFound;
for(int i =0; i < firstdropdown.size; i++)
{
      valueFound = false;
      string firstValue = firstdropdown.getelement(i).value();
      for(int j =0; j < seconddropdown.size; j++)
      {
            string secondValue = seconddropdown.getelement(j).value
            if((firstValue.equals(secondValue))
            {
                 valueFound = true;
                 break;
            }
      }
      if(!valueFound)
      {
             //Report the message
             break;       // Add this.
      }
}

Hope this helps.

Regards,

Manoj

Former Member
0 Kudos

Hi Manoj,

it worked well... thanks very much...

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

Colud you please tell what non relevant values you are getting? Are you sure that you have written 0(zero) and not o(alphabet) in your for loop? Please re-check.

Regards,

Manoj

Former Member
0 Kudos

Hi Try this Code

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

String found = "False";

string abc = firstdropdown.getelement(a).value

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

string xyz= seconddropdown.getelement(b).value

if(abc .equals(xyz)

{

found = "true"

}

}

if("false".equalingonecase(found))

{

Message Not found

}

else

{

Message found

}

Regards,

Raju Bonagiri

}