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 2122
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.
JosAH 11,448
Recognized Expert MVP
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(err ors.get("Joe")) ;
or
out.println(err ors.get(firstna me)); 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="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.
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?
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)
{
int result = 0;
for(int i = 1;i <=iterations;i++)
{
result += i;
|
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 median=66
for (i=0;i<N;i++){
R+=A*B*K; // all variables are float=4 bytes
|
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 < myArray.Length - 1; i++)
{
// code here
}
Is "myArray.Length - 1" calculated once, or on each pass through the loop?
|
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 spend twice the time to
complete. If the loop is included it takes about 7.48 seconds to
complete, but when removed it takes about 11.48 seconds.
Does anybody have a suggestion as to why this is so and whether I can
trust the results of the...
|
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 it to use
memset and I get totally different results.. How can this be?
Here is the example:
float gaborfilter;
| |
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 that loops through all the radiobuttonlists and puts
the selected answers in the string array that i've created. I guess this is
relatively easy, but I haven't figured it out..here's the code that I have
for now:
protected void...
|
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 preceeding variable as well.
System information
Server
Apache/2.2.4 (Win32) DAV/2 mod_ssl/2.2.4 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.3 Time: 12/07/2007 01:20:06 PM
PHP
PHP Version: 5.2.3 Safe Mode activated: no PHP Memory...
|
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 problem I've stumbled upon is that I don't know how to do what I
want. I want to do a loop in a loop. I think.
|
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 finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:
heights=;
function getElevationInter(latv,lngv) {
var script =...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |