Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HashMap and Array issue

0 views
Skip to first unread message

tes...@hotmail.com

unread,
Aug 14, 2007, 9:23:56 PM8/14/07
to
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.

tes...@hotmail.com

unread,
Aug 14, 2007, 9:35:13 PM8/14/07
to

tes...@hotmail.com

unread,
Aug 14, 2007, 9:35:29 PM8/14/07
to

Lew

unread,
Aug 14, 2007, 10:09:03 PM8/14/07
to
tes...@hotmail.com wrote:
> 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();

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.

--
Lew

Oliver Wong

unread,
Aug 15, 2007, 4:38:08 PM8/15/07
to

<tes...@hotmail.com> wrote in message
news:1187141036.8...@j4g2000prf.googlegroups.com...

>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"));
> %>
>
>
[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/bbdb697ccfa392ea/8662e0b6d480896c?lnk=raot

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

- Oliver


0 new messages