473,396 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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):
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 2109
nickyeng
254 100+
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 Expert 8TB
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
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(errors.get("Joe"));

or
out.println(errors.get(firstname)); i e without double quotes to get the output.
Aug 15 '07 #4
gaya3
184 100+
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="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.
Aug 27 '07 #5
r035198x
13,262 8TB
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?
Aug 27 '07 #6

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

Similar topics

47
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) {...
7
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...
8
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 <...
102
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...
22
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...
6
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...
23
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
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...
23
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...
15
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...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
jinu1996
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...
0
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...
0
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...

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.