Loop and Array issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oaklander
    New Member
    • Aug 2007
    • 20

    Loop and Array issue

    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):
    Code:
    <%@ 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:
    Code:
    <%@ 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.
  • nickyeng
    Contributor
    • Nov 2006
    • 252

    #2
    Hi there,

    try this

    Code:
    for(int i = 0;i < keys.length;i++)
          {
      
              if(!keys[i].equals(""))
      
              {
      
                  errors.put(keys[i],keys[i]);
     	      out.println(errors.get(keys[i]));   // put in the for loop inside.
      
              }
          }

    it runs well and print the value.

    you shouldn't put 'firstname' and 'lastname' in out.print()...

    if you put field into array, its the best to do is use array[0] , array[1] and so on.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      You associate "Joe" with "Joe" and "Miller" with "Miller" in your HashMap. You
      can't be surprised that the key "firstname" can not be found in that map.

      kind regards,

      Jos

      Comment

      • misaw
        New Member
        • Aug 2007
        • 17

        #4
        Originally posted by oaklander
        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):
        Code:
        <%@ 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:
        Code:
        <%@ 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.
        firstname is a string variable containing "Joe" and in loop you actually inserted the key having value "Joe" not the "firstname"

        either try this
        out.println(err ors.get("Joe")) ;

        or
        out.println(err ors.get(firstna me)); i e without double quotes to get the output.

        Comment

        • gaya3
          New Member
          • Aug 2007
          • 184

          #5
          Originally posted by oaklander
          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):
          Code:
          <%@ 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:
          Code:
          <%@ 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.
          Hi,
          Following code may help u...
          import java.util.*;
          class InputLines
          {
          public static void main(String args[])
          {
          HashMap errormap = new HashMap();
          String firstname="joe" ;
          String lastname="mille r";
          String[] keys ={firstname,las tname};
          for(int i=0;i<keys.leng th;i++)
          {
          errormap.put(ke ys[i],keys[i]);
          }
          System.out.prin tln("firstname: "+errormap.get( firstname));
          System.out.prin tln("lastname:" +errormap.get(l astname));

          }
          }



          u have to get like errormap.get(fi rstname).. wer firstname string value is considered as key in hashmap...


          -Thanks,
          Hamsa.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by gaya3
            Hi,
            Following code may help u...
            import java.util.*;
            class InputLines
            {
            public static void main(String args[])
            {
            HashMap errormap = new HashMap();
            String firstname="joe" ;
            String lastname="mille r";
            String[] keys ={firstname,las tname};
            for(int i=0;i<keys.leng th;i++)
            {
            errormap.put(ke ys[i],keys[i]);
            }
            System.out.prin tln("firstname: "+errormap.get( firstname));
            System.out.prin tln("lastname:" +errormap.get(l astname));

            }
            }



            u have to get like errormap.get(fi rstname).. wer firstname string value is considered as key in hashmap...


            -Thanks,
            Hamsa.
            1.) Please use code tags when posting code
            2.) Did you even test this code of yours to see what it does when it's run?

            Comment

            Working...