Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

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
0
jmc430
Asked:
jmc430
  • 4
  • 4
  • 4
  • +1
3 Solutions
 
objectsCommented:
>  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?
0
 
jmc430Author Commented:
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!  :)
0
 
objectsCommented:
add some println() statements to your loop to get a better idea of whats happenning
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
Maybe addresses after the first one are not External, On Campus or Office?
0
 
objectsCommented:
> 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;
                }
0
 
objectsCommented:
If you actually want to process all rows regardless of type 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";
                }
0
 
CEHJCommented:
>>Already pointed out that earlier

Where?
0
 
nadhuviCommented:
I think u need to print the value for   loc.locationType()
bcz i suspect this is containting the vaue you a re expecting so it s giving u unexpected result.

plz check it out
0
 
jmc430Author Commented:
it's finding 3 "On Campus" locations for loc.locationType().. but is only printing out one location.

how would I fix this?
0
 
CEHJCommented:
Certainly the logic is rather puzzling. Can you explain, e.g., about addressType and locationType?
0
 
jmc430Author Commented:
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!
0
 
CEHJCommented:
:-)

It would be useful to explain for future visitors what you did
0
 
jmc430Author Commented:
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!

:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now