473,794 Members | 2,880 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loop and Array issue

20 New Member
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):
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" import="java.util.*"  %>
  2. <%
  3. HashMap errors = new HashMap();
  4. String firstname = "Joe";
  5. String lastname = "Miller";
  6.  
  7.     if (!firstname.equals(""))
  8.     {
  9.         errors.put("firstname",firstname);
  10.     }
  11.     if (!lastname.equals(""))
  12.     {
  13.         errors.put("lastname",lastname);
  14.     }
  15.  
  16. out.println(errors.get("firstname"));
  17. out.println(errors.get("lastname"));
  18. %>
  19.  
It prints out Joe Miller

Now my attempt below to put this in a loop prints out null null:
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" import="java.util.*"  %>
  2. <%
  3. HashMap errors = new HashMap();
  4. String firstname = "Joe";
  5. String lastname = "Miller";
  6. //String[] keys = {"firstname", "lastname"};
  7. String[] keys = {firstname, lastname};
  8. for(int i = 0;i < keys.length;i++)
  9. {
  10.      if(!keys[i].equals(""))
  11.     {
  12.         errors.put(keys[i],keys[i]);
  13.     }
  14. }
  15.  
  16. out.println(errors.get("firstname"));
  17. out.println(errors.get("lastname"));
  18.  
  19. %>
  20.  

Please advise.
Aug 15 '07 #1
5 2122
nickyeng
254 Contributor
Hi there,

try this

Expand|Select|Wrap|Line Numbers
  1. for(int i = 0;i < keys.length;i++)
  2.       {
  3.  
  4.           if(!keys[i].equals(""))
  5.  
  6.           {
  7.  
  8.               errors.put(keys[i],keys[i]);
  9.            out.println(errors.get(keys[i]));   // put in the for loop inside.
  10.  
  11.           }
  12.       }
  13.  

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.
Aug 15 '07 #2
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
Aug 15 '07 #3
misaw
17 New Member
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):
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" import="java.util.*"  %>
  2. <%
  3. HashMap errors = new HashMap();
  4. String firstname = "Joe";
  5. String lastname = "Miller";
  6.  
  7.     if (!firstname.equals(""))
  8.     {
  9.         errors.put("firstname",firstname);
  10.     }
  11.     if (!lastname.equals(""))
  12.     {
  13.         errors.put("lastname",lastname);
  14.     }
  15.  
  16. out.println(errors.get("firstname"));
  17. out.println(errors.get("lastname"));
  18. %>
  19.  
It prints out Joe Miller

Now my attempt below to put this in a loop prints out null null:
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" import="java.util.*"  %>
  2. <%
  3. HashMap errors = new HashMap();
  4. String firstname = "Joe";
  5. String lastname = "Miller";
  6. //String[] keys = {"firstname", "lastname"};
  7. String[] keys = {firstname, lastname};
  8. for(int i = 0;i < keys.length;i++)
  9. {
  10.      if(!keys[i].equals(""))
  11.     {
  12.         errors.put(keys[i],keys[i]);
  13.     }
  14. }
  15.  
  16. out.println(errors.get("firstname"));
  17. out.println(errors.get("lastname"));
  18.  
  19. %>
  20.  

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.
Aug 15 '07 #4
gaya3
184 New Member
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):
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" import="java.util.*"  %>
  2. <%
  3. HashMap errors = new HashMap();
  4. String firstname = "Joe";
  5. String lastname = "Miller";
  6.  
  7.     if (!firstname.equals(""))
  8.     {
  9.         errors.put("firstname",firstname);
  10.     }
  11.     if (!lastname.equals(""))
  12.     {
  13.         errors.put("lastname",lastname);
  14.     }
  15.  
  16. out.println(errors.get("firstname"));
  17. out.println(errors.get("lastname"));
  18. %>
  19.  
It prints out Joe Miller

Now my attempt below to put this in a loop prints out null null:
Expand|Select|Wrap|Line Numbers
  1. <%@ page language="java" import="java.util.*"  %>
  2. <%
  3. HashMap errors = new HashMap();
  4. String firstname = "Joe";
  5. String lastname = "Miller";
  6. //String[] keys = {"firstname", "lastname"};
  7. String[] keys = {firstname, lastname};
  8. for(int i = 0;i < keys.length;i++)
  9. {
  10.      if(!keys[i].equals(""))
  11.     {
  12.         errors.put(keys[i],keys[i]);
  13.     }
  14. }
  15.  
  16. out.println(errors.get("firstname"));
  17. out.println(errors.get("lastname"));
  18.  
  19. %>
  20.  

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.
Aug 27 '07 #5
r035198x
13,262 MVP
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?
Aug 27 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

47
12339
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;
7
2693
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
8
3017
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?
102
4569
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...
22
26746
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;
6
7270
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...
23
13333
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
1
3036
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...
23
1732
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.
15
2368
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 =...
0
9672
marktang
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...
0
10435
Oralloy
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...
1
10163
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,...
0
10000
tracyyun
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...
0
6779
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();...
0
5436
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...
1
4113
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
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.