HashMap and Array issue

Discussion in 'Java' started by teser3@hotmail.com, Aug 15, 2007.

  1. Guest

    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.equals(""))
    {
    errors.put(keys,keys);
    }
    }

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

    %>

    Please advise.
     
    , Aug 15, 2007
    #1
    1. Advertising

  2. Lew Guest

    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.equals(""))
    > {
    > errors.put(keys,keys);


    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
     
    Lew, Aug 15, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince Darley
    Replies:
    4
    Views:
    4,548
    emilchacko
    Mar 2, 2010
  2. Replies:
    1
    Views:
    288
    Oliver Wong
    Aug 15, 2007
  3. Replies:
    0
    Views:
    323
  4. Rakesh
    Replies:
    10
    Views:
    12,265
    Mike Schilling
    Apr 8, 2008
  5. jagadesh

    HashMap Issue

    jagadesh, May 14, 2009, in forum: Java
    Replies:
    11
    Views:
    980
    Roedy Green
    May 15, 2009
Loading...

Share This Page