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:23 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.
Oliver Wong - 15 Aug 2007 21:38 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 - 18 lines]
> out.println(errors.get("lastname"));
> %>

[Snip - summary: wants to invole arrays and a for loop somehow]

   This is almost identical to the question you asked earlier:
http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/bb
db697ccfa392ea/8662e0b6d480896c?lnk=raot


   Please don't force people to repeat their answers to you.

   - Oliver


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.