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 ("location s");
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.loca tionType() ) ) {
b = street1+"\n"+street2+"\n"+ city+", "+state+" "+zip+"\n";
} if ( "On Campus".equals(loc.locatio nType()) ){
b += street1+"\n"+street2+"\n"+ city+", "+state+" "+zip+"\n";
} if ( "Office".equals(loc.addres sType()) ){
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
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
java.util.Enumeration e = pc.objectEnumerator();
Session s = (Session)editingContext().
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.loca
b = street1+"\n"+street2+"\n"+
} if ( "On Campus".equals(loc.locatio
b += street1+"\n"+street2+"\n"+
} if ( "Office".equals(loc.addres
b += street1+"\n"+street2+"\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
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! :)
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.loca tionType() ) ) {
b += street1+"\n"+street2+"\n"+ city+", "+state+" "+zip+"\n";
} else if ( "On Campus".equals(loc.locatio nType()) ){
b += street1+"\n"+street2+"\n"+ city+", "+state+" "+zip+"\n";
} else if ( "Office".equals(loc.addres sType()) ){
b += street1+"\n"+street2+"\n"+ city+", "+state+" "+zip+"\n";
} else return b;
}
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.loca
b += street1+"\n"+street2+"\n"+
} else if ( "On Campus".equals(loc.locatio
b += street1+"\n"+street2+"\n"+
} else if ( "Office".equals(loc.addres
b += street1+"\n"+street2+"\n"+
} else return b;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>Already pointed out that earlier
Where?
Where?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it's finding 3 "On Campus" locations for loc.locationType().. but is only printing out one location.
how would I fix this?
how would I fix this?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
thanks to all of you for your help! i really appreciate it!
:-)
It would be useful to explain for future visitors what you did
It would be useful to explain for future visitors what you did
ASKER
I used this:
if ((loc.locationType().equal s("Externa l"))) {
locations = locations +street1+"\n"+street2+"\n" +city+", "+state+" "+zip+"\n";
}
if ((loc.locationType().equal s("On Campus"))){
locations = locations +"\n"+ street1+"\n"+street2+"\n"+ city+", "+state+" "+zip+"\n";
}
and it miraculously worked!
:)
if ((loc.locationType().equal
locations = locations +street1+"\n"+street2+"\n"
}
if ((loc.locationType().equal
locations = locations +"\n"+ street1+"\n"+street2+"\n"+
}
and it miraculously worked!
:)
have you verified that?
> b = street1+"\n"+street2+"\n"+
should that be:
b += (street1+"\n"+street2+"\n"
> } else return b;
whats the purpose of this?