?
Solved

Array list not returning properly

Posted on 2005-03-10
13
Medium Priority
?
188 Views
Last Modified: 2010-03-31
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
Comment
Question by:jmc430
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +1
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13511005
>  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
 

Author Comment

by:jmc430
ID: 13511129
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
 
LVL 92

Expert Comment

by:objects
ID: 13511955
add some println() statements to your loop to get a better idea of whats happenning
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Expert Comment

by:CEHJ
ID: 13512906
Maybe addresses after the first one are not External, On Campus or Office?
0
 
LVL 92

Expert Comment

by:objects
ID: 13513005
> 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
 
LVL 92

Accepted Solution

by:
objects earned 900 total points
ID: 13513014
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13515046
>>Already pointed out that earlier

Where?
0
 
LVL 1

Assisted Solution

by:nadhuvi
nadhuvi earned 200 total points
ID: 13515706
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
 

Author Comment

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

how would I fix this?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 900 total points
ID: 13519696
Certainly the logic is rather puzzling. Can you explain, e.g., about addressType and locationType?
0
 

Author Comment

by:jmc430
ID: 13520483
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13520503
:-)

It would be useful to explain for future visitors what you did
0
 

Author Comment

by:jmc430
ID: 13521382
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question