Link to home
Start Free TrialLog in
Avatar of jmc430
jmc430

asked on

Array list not returning properly

Greetings!

I have a question concerning array lists.   I am wondering if my logic is inherently wrong -- I am trying to return a list of addresses, but only one list is being returned, though I know the results are supposed to have at least 3 addresses per person.

Please look at my code:

public String getStreet() {
        NSArray pc = (NSArray)storedValueForKey("locations");
        java.util.Enumeration e = pc.objectEnumerator();
        Session s = (Session)editingContext().delegate();
        String street1 = "", street2 = "", b="", city="", state="", zip="";

        while (e.hasMoreElements()){
            Location loc = (Location)e.nextElement();
           
                        if ((loc.street1()!= null)&&(( loc.locationType() != null )||( loc.addressType() != null ))){

                            street1 = loc.street1();
                         if (loc.street2()!=null){
                            street2 = loc.street2();
                        }else street2="";
                            if (loc.city()!=null){
                                city=loc.city();
                        } else city="";
                        if (loc.state()!=null){
                            state = loc.state();
                        }else state="";
                            if (loc.zip()!=null){
                            zip = loc.zip();
                        }else zip="";

                if ( "External".equals(loc.locationType()) ) {
                        b = street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n";
                } if ( "On Campus".equals(loc.locationType()) ){
                       b += street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n";
                } if ( "Office".equals(loc.addressType()) ){
                       b += street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n";
                } else return b;
                }
                }
        return b;
    }

Currently, this is only returning:
123 E St.
Rm. 1234
Mail Box: 1234

Whereas I would like this to be returned (or as many results applicable):
123 E St.
Rm. 1234
Mail Box: 1234
456 E St.
Rm. 4567
Mail Box: 4567

Is there something inherently wrong with the way I am trying to retrieve the data?  I am very confused.  Please help!

Any advice or guidance is greatly appreciated!

Thanks!

Best regards,
Jamie
Avatar of Mick Barry
Mick Barry
Flag of Australia image

>  though I know the results are supposed to have at least 3 addresses per person.

have you verified that?

>                    b = street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n";


should that be:

                   b += (street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n");

>               } else return b;

whats the purpose of this?
Avatar of jmc430
jmc430

ASKER

I'm creating a Printable Department Directory ... kind of like an online phone book.

I have verified that, though not in all cases, there exist situations where more than one address exists, yet my code is only returning one address result per person.

I have verified this because I have implemented a page that correctly  returns all results on the browser window (an HTML page) along with a corresponding PDF report (incorrect results).  My PDF only prints out 1 address per person.  The function I posted returns results onto the PDF template.

I changed the "b=" to "b+=". .. to no avail.. is my logic flawed?  

Thanks for writing me back!  :)
add some println() statements to your loop to get a better idea of whats happenning
Maybe addresses after the first one are not External, On Campus or Office?
> Maybe addresses after the first one are not External, On Campus or Office?

Already pointed out that earlier, though thats not actually the logic being used.
If the second row is not of type office then it will return thus only displaying the first record. If you want to check if not Office/External or On Campus then use:

                if ( "External".equals(loc.locationType()) ) {
                        b += street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n";
                } else if ( "On Campus".equals(loc.locationType()) ){
                       b += street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n";
                } else if ( "Office".equals(loc.addressType()) ){
                       b += street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n";
                } else return b;
                }
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Already pointed out that earlier

Where?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmc430

ASKER

it's finding 3 "On Campus" locations for loc.locationType().. but is only printing out one location.

how would I fix this?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmc430

ASKER

i finally figured it out .. i'm not sure what i did wrong the first time though.

thanks to all of you for your help!  i really appreciate it!
:-)

It would be useful to explain for future visitors what you did
Avatar of jmc430

ASKER

I used this:

 if ((loc.locationType().equals("External"))) {

                    locations = locations +street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n";
                }
                if ((loc.locationType().equals("On Campus"))){  

                    locations = locations +"\n"+ street1+"\n"+street2+"\n"+city+", "+state+" "+zip+"\n";
                }  
and it miraculously worked!

:)