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.
5 2109
Hi there,
try this -
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.
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
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.
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(errors.get("Joe"));
or
out.println(errors.get(firstname)); i e without double quotes to get the output.
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.
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="miller";
String[] keys ={firstname,lastname};
for(int i=0;i<keys.length;i++)
{
errormap.put(keys[i],keys[i]);
}
System.out.println("firstname:"+errormap.get(first name));
System.out.println("lastname:"+errormap.get(lastna me));
}
}
u have to get like errormap.get(firstname).. wer firstname string value is considered as key in hashmap...
-Thanks,
Hamsa.
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="miller";
String[] keys ={firstname,lastname};
for(int i=0;i<keys.length;i++)
{
errormap.put(keys[i],keys[i]);
}
System.out.println("firstname:"+errormap.get(first name));
System.out.println("lastname:"+errormap.get(lastna me));
}
}
u have to get like errormap.get(firstname).. 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?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mountain Bikn' Guy |
last post by:
Take some standard code such as shown below. It simply loops to add up a
series of terms and it produces the correct result.
// sum numbers with a loop
public int DoSumLooping(int iterations)
{...
|
by: Rajeev |
last post by:
Hello,
I'm using gcc 3.4.2 on a Xeon (P4) platform, all kinds of speed optimizations
turned on.
For the following loop
R=(evaluate here); // float
N=(evaluate here); // N min=1 max=100...
|
by: Dave Veeneman |
last post by:
In a for-loop, is a calculated expression re-calculated on each pass through
the loop, or only once, when the loop is initialized? For example, assume
the following loop:
for (int i = 0; i <...
|
by: tom fredriksen |
last post by:
Hi
I was doing a simple test of the speed of a "maths" operation and when I
tested it I found that removing the loop that initialises the data array
for the operation caused the whole program to...
|
by: silversurfer2025 |
last post by:
Hello everybdy,
I am a little confused for the following reason:
In my code I used a simple for-loop in order to initialize a 2D-array
of floats to zero. Because of efficiency reasons, I changed...
|
by: Svein Erik |
last post by:
C# asp.net 2.0.
I'm creating an online survey. I'm making a string array that's holding the
variables of the answers made in the radiobuttonlists that i create manually.
I need to make a method...
|
by: AndersWang |
last post by:
Hi,
dose anybody here explain to me why memset would be faster than a
simple loop. I doubt about it!
In an int array scenario:
int array;
for(int i=0;i<10;i++) //ten loops
|
by: djmeltdown |
last post by:
I'm having trouble getting a foreach() loop to insert a record into a MySQL Database. Which I've never had any trouble before, it just seems quirky.
I've tried the mysql_query statement without a...
|
by: Sacred Heart |
last post by:
Hi,
I'm new to Python and have come across a problem I don't know how to
solve, enter com.lang.python :)
I'm writing some small apps to learn the language, and I like it a lot
so far.
My...
|
by: Steve |
last post by:
I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |