Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

HashMap and Array issue

Thread view: 
teser3@hotmail.com - 15 Aug 2007 02:35 GMT
I have this JSP where I have alot of fields with conditions.
I would like to make it more efficient and use a for loop.
Here is an example (showing 2 fields for example only):

<%@ page language="java" import="java.util.*"  %>
<%
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";

   if (!firstname.equals(""))
   {
       errors.put("firstname",firstname);
   }
   if (!lastname.equals(""))
   {
       errors.put("lastname",lastname);
   }

out.println(errors.get("firstname"));
out.println(errors.get("lastname"));
%>

It prints out Joe Miller

Now my attempt below to put this in a loop prints out null null:

<%@ page language="java" import="java.util.*"  %>
<%
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";
//String[] keys = {"firstname", "lastname"};
String[] keys = {firstname, lastname};
for(int i = 0;i < keys.length;i++)
{
    if(!keys[i].equals(""))
   {
       errors.put(keys[i],keys[i]);
   }
}

out.println(errors.get("firstname"));
out.println(errors.get("lastname"));

%>

Please advise.
Lew - 15 Aug 2007 03:09 GMT
> I have this JSP where I have alot of fields with conditions.
> I would like to make it more efficient and use a for loop.
[quoted text clipped - 26 lines]
> <%
> HashMap errors = new HashMap();

or better: Map errors = new HashMap();

> String firstname = "Joe";
> String lastname = "Miller";

> String[] keys = {firstname, lastname};

This array contains {"Joe", "Miller"}

> for(int i = 0;i < keys.length;i++)
> {
>      if(!keys[i].equals(""))
>     {
>         errors.put(keys[i],keys[i]);

This will insert the <K, V> pairs <"Joe", "Joe"> and <"Miller", "Miller"> into
the Map.

You're using the exact same value for both the key and the value of each
Map.Entry.

>     }
> }
>
> out.println(errors.get("firstname"));

A better idiom is
<%= errors.get( "firstname" ) %>

"firstname" was never entered into the Map as a key, only "Joe" and "Miller".

> out.println(errors.get("lastname"));

"lastname" was never entered into the Map as a key, only "Joe" and "Miller".

> %>

After you get the hang of doing this in a JSP, figure out how to move all Java
source out of the JSP and into logic classes invoked from a servlet.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.