Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array reference not working in put method

Status
Not open for further replies.

evergrean100

Technical User
Dec 1, 2006
115
0
0
US
I have a validate method that works and now I want to condense it into a for loop.

Here is what I have:
Code:
 //firstname and lastname are declared earlier
 public boolean validate()
 {
    boolean allOk=true;
    if (firstName.equals("")) {
      errors.put("firstName","Please enter your first name");
      allOk=false;
    }
    if (lastName.equals("")) {
      errors.put("lastName","Please enter your last name");
      allOk=false;
    }
    return allOk;
  }

My attempt doesnt work because I cant seem to figure out how to put the array reference in the key part of the errors.put method:
Code:
 public boolean validate()
 {
    boolean allOk=true;
    String [] myarray = {firstname, lastname}
    for(int i=0;i < myarray.length;i++)
    {
    if (myarray[i].equals("")) {
      errors.put(myarray[i],"Please enter your " + myarray[i]);
      allOk=false;
    }
    }
    return allOk;
  }

The results dont validate any of my data so I assume I have something wrong with my array. Please advise.
 
I think the error is in your array declaration:

Shouldn't it be this?

Code:
String[] myArray = [b]new String[][/b] {firstname, lastname};

Hope this helps,

Alex

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Whoops, maybe I should have actually tried the way you are doing it first. I don't use java at work so it is a huge nuisance to compile anything here :-(

How are firstname and lastname declared? Within a class or a particular method?



[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
In your second code example, you're attempting to use myarray in three different ways, judging by the original code.

As the String to validate in myarray.equals, which will work.
As a String for the first param to errors.put, which in the original was a String of the name of the String that was empty, which won't work.
As a String for the end of the second param to errors.put, which in the original was the English description of the String that was empty, which won't work.

Since your if block's conditional is checking for an empty string, the first line of the block will always evaluate as
Code:
errors.put("", Please enter your ");

- Rod




IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

A Simple Code for Posting on the Web
 
Here is how it is used in a JSP showing just 2 fields for example:
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"));
%>

It works great and prints out Joe Miller

Now my attempt 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 how I can get it to work in a loop because I have spent alot of time trying to figure this out. Thanks.
 
you shouldn't use any Java code in JSPs, only JSTL and custom taglibs...

But as to your question: do look closely.
What are you putting into your Map the second time around?
Is there even an entry "firstname" in there? Or an entry "lastname"?

The answer of course is a resounding NO. Your Map now contains entries "Joe","Joe" and "Miller","Miller".
Probably not your intention, but that's what you put in there.
 
This is what you're trying yo get, I guess:

Code:
<%@ page language="java" import="java.util.*"  %>
<%
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";
String[] keys = {"First Name", "Last Name"};
String[] values = {"firstname", "lastname"};
for(int i = 0;i < values.length;i++)
{
     if(!values[i].equals(""))
    {
        errors.put(keys[i],values[i]);
    }
}

out.println(errors.get("First Name"));
out.println(errors.get("Last Name"));
%>

Btw, jwenting, what's wrong with Java code in JSPs?

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top