how to show a specific value in an option

Hi I am using an option in order to help the user to select a particular country. What I need is that if a user needs to edit a specific record the option should show the country name which the user had entered before

Here is my code

<td>
                  <select name="COUNTRY" class="textfield">
                  
            <option value='0' selected> 'Please select one'</option>
<%
                        for(int key_index = 0; key_index < country_keys.length; key_index++) {
                              String next_key   = (String)country_keys[key_index];
                              String next_value = (String)countries.get(next_key);
                              String selected_country = "";
                              if(selected_event.getCountry() == (null) || selected_event.getCountry() == "") { selected_country = "selected"; }
                              else                                                   { selected_country = "";}
%>
                              <option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%                        } %>
                  </select>
            </td>

In this example if there is a value for selected_event.getCountry than the option should show that value, in other words it should be selected. Right now it shows the last country by default

Any help will be greatly appreciated
royalcyberAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
As a pure guess:

    <td>
       <select name="COUNTRY" class="textfield">
               
          <option value='0' selected> 'Please select one'</option>
<%
                    for( int key_index = 0; key_index < country_keys.length ; key_index++ )  
                    {
                         String next_key   = (String)country_keys[key_index];
                         String next_value = (String)countries.get(next_key);
                         String selected_country = "";
                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_value ) )
                         {
                             selected_country = "selected";
                         }
                         else                                                  
                         {
                             selected_country = "";
                         }
%>
                         <option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%                
                    }
%>
               </select>
          </td>
TimYatesCommented:
it was this line:

                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_value ) )

that I changed...

It probably should be:

                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_key ) )

But you don't say exactly what selected_event is...

Tim
royalcyberAuthor Commented:
I have actually stored the countries in a LinkedHashMap
 
LinkedHashMap countries = (LinkedHashMap)request.getAttribute("COUNTRIES");

next_key = key of hashmap
next_value = value of hashmap

Sorry it was a typo the real code was like this

    <td>
       <select name="COUNTRY" class="textfield">
               
          <option value='0' selected> 'Please select one'</option>
<%
                    for( int key_index = 0; key_index < country_keys.length ; key_index++ )  
                    {
                         String next_key   = (String)country_keys[key_index];
                         String next_value = (String)countries.get(next_key);
                         String selected_country = "";
                         if( selected_event.getCountry() == (null) && selected_event.getCountry().equals("") )
                         {
                             selected_country = "";
                         }
                         else                                                  
                         {
                             selected_country = "selected";
                         }
%>
                         <option value="<%= next_key %>" <%= selected_country %>><%= next_value %></option>
<%                
                    }
%>
               </select>
          </td>
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
                      if( selected_event.getCountry() == (null) || selected_event.getCountry().equals("") )
basically change the above if satement as foliows & where are you setting selected_event????
TimYatesCommented:
As I said, shouldn't:

                         if( selected_event.getCountry() == (null) && selected_event.getCountry().equals("") )
                         {
                             selected_country = "";
                         }
                         else                                                  
                         {
                             selected_country = "selected";
                         }

be:

                         if( selected_event.getCountry() != null && selected_event.getCountry().equals( next_value ) )
                         {
                             selected_country = "selected";
                         }
                         else                                                  
                         {
                             selected_country = "";
                         }

?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ManishLeadCommented:
<option value='0'> 'Please select one'</option>
<%
                    for( int key_index = 0; key_index < country_keys.length ; key_index++ )  
                    {
                         String next_key   = (String)country_keys[key_index];
                         String next_value = (String)countries.get(next_key);
                         String selected_country = "";
                         if( selected_event.getCountry() != (null) && selected_event.getCountry().equals( next_value ) )
                         { %>
  <option value="<%= next_key %>" selected><%= next_value %></option>

                <%  } %>
                         <option value="<%= next_key %>"><%= next_value %></option>
<%                
                    }
%>
               </select>



karan

TimYatesCommented:
that's what I said with the code shuffled a bit... :-/
ManishLeadCommented:
sorry..

%  } else %>
           <option value="<%= next_key %>"><%= next_value %></option>
<%                
              }
%>
Ya tim , u r right..!
royalcyberAuthor Commented:
Thank you very much guys
TimYatesCommented:
Glad to be of help :-)

Good luck!

Tim
ManishLeadCommented:
:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.