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):
It prints out Joe Miller
Now my attempt below to put this in a loop prints out null null:
Please advise.
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")); %>
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.
Comment