Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Array reference in for loop

Thread view: 
teser3@hotmail.com - 14 Aug 2007 01:00 GMT
I have a validate method in a bean class that works and now I want to
condense it into a for loop.

Here is what I have:

//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:

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.
GArlington - 14 Aug 2007 11:36 GMT
On 14 Aug, 01:00, "tes...@hotmail.com" <tes...@hotmail.com> wrote:
> I have a validate method in a bean class that works and now I want to
> condense it into a for loop.
[quoted text clipped - 35 lines]
> The results dont validate any of my data so I assume I have something
> wrong with my array.  Please advise.

Is this your actual code? Did you even try to compile it?
What does the compiler say about your line
String [] myarray = {firstname, lastname}
???
I can only guess (thanks to your selective code pasting) that
you are atempting
String [] myarray = {firstName, lastName};
or
String [] myarray = {getFirstName(), getLastName()};
IchBin - 14 Aug 2007 17:09 GMT
> I have a validate method in a bean class that works and now I want to
> condense it into a for loop.
[quoted text clipped - 35 lines]
> The results dont validate any of my data so I assume I have something
> wrong with my array.  Please advise.

Quick and dirty:

    public boolean validate()
    {
        boolean allOk=true;
        String [] myarray = {firstName, lastName,
                            "first Name","last Name"};
        for(int i=0;i < myarray.length;i++){
            if (myarray[i].equals("")) {
                errors.put(myarray[i] + " Please enter your " +
                                          myarray[i+2]);
                allOk=false;
            }
        }
        return allOk;
    }

Signature

Thanks in Advance...                           http://weconsulting.org
IchBin, Philadelphia, Pa, USA http://ichbinquotations.weconsulting.org
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

Patricia Shanahan - 14 Aug 2007 17:15 GMT
>> I have a validate method in a bean class that works and now I want to
>> condense it into a for loop.
[quoted text clipped - 52 lines]
>         return allOk;
>     }

There are two needs for a String array, providing the labels for using
in the message and holding the input. Why not use two arrays, one for
each job?

    public boolean validate()
    {
        boolean allOk=true;
        String[] input = {firstName, lastName};
        String[] labels = {"first Name","last Name"};
        for(int i=0;i < input.length;i++){
            if (input[i].equals("")) {
                errors.put(input[i] + " Please enter your " +
                                          labels[i]);
                allOk=false;
            }
        }
        return allOk;
    }

I don't like names like "myarray", because they are often associated
with constructs that do not have a single clear purpose.

Patricia
Daniel Pitts - 14 Aug 2007 21:21 GMT
> > tes...@hotmail.com wrote:
> >> I have a validate method in a bean class that works and now I want to
[quoted text clipped - 77 lines]
>
> Patricia

I don't like parallel arrays myself, and I don't think this is a job
for an array actually.

public void validateHelper(String fieldName, String fieldValue, String
message) {
  if (fieldValue == null || fieldValue.equals("")) {
      errors.put(fieldName, fieldMessage);
  }
}

public void validate() {
  validateHelper("firstName", firstName, "Please enter your first
name.");
  validateHelper("lastName", lastName, "Please enter your last
name.");
}
Patricia Shanahan - 14 Aug 2007 21:31 GMT
>>>tes...@hotmail.com wrote:
>>>
[quoted text clipped - 95 lines]
> name.");
> }

Yes, that is a better design.

Patricia
cyprian - 22 Aug 2007 11:55 GMT
> >>>tes...@hotmail.com wrote:
>
[quoted text clipped - 99 lines]
>
> Patricia

there is nothing wrong with your array if firstname and secondname are
visible. See if the problem is with the strings you're passing the put
method. Maybe do String toPut = "the words"+myArray[i];
then make one of put's parameters toPut rather than that string
literal.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.