Array reference in for loop  
Author Message
teser3@hotmail.com





PostPosted: 2007-8-14 8:00:00 Top

java-programmer, Array reference in for loop 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





PostPosted: 2007-8-14 18:37:00 Top

java-programmer >> Array reference in for loop On 14 Aug, 01:00, "email***@***.com" <email***@***.com> wrote:
> 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.

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





PostPosted: 2007-8-15 0:10:00 Top

java-programmer >> Array reference in for loop email***@***.com wrote:
> 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.
>

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;
}

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





PostPosted: 2007-8-15 0:16:00 Top

java-programmer >> Array reference in for loop IchBin wrote:
> email***@***.com wrote:
>> 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.
>>
>
> 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;
> }
>

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





PostPosted: 2007-8-15 4:21:00 Top

java-programmer >> Array reference in for loop On Aug 14, 9:15 am, Patricia Shanahan <email***@***.com> wrote:
> IchBin wrote:
> > email***@***.com wrote:
> >> 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.
>
> > 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;
> > }
>
> 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

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





PostPosted: 2007-8-15 4:31:00 Top

java-programmer >> Array reference in for loop Daniel Pitts wrote:
> On Aug 14, 9:15 am, Patricia Shanahan <email***@***.com> wrote:
>
>>IchBin wrote:
>>
>>>email***@***.com wrote:
>>>
>>>>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.
>>
>>>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;
>>> }
>>
>>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
>
>
> 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.");
> }
>
>

Yes, that is a better design.

Patricia
 
 
cyprian





PostPosted: 2007-8-22 18:55:00 Top

java-programmer >> Array reference in for loop On Aug 14, 4:31 pm, Patricia Shanahan <email***@***.com> wrote:
> Daniel Pitts wrote:
> > On Aug 14, 9:15 am, Patricia Shanahan <email***@***.com> wrote:
>
> >>IchBin wrote:
>
> >>>email***@***.com wrote:
>
> >>>>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.
>
> >>>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;
> >>> }
>
> >>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
>
> > 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.");
> > }
>
> Yes, that is a better design.
>
> 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.