I have a issue with my java code. i asked the same question yesterday. I got answer but sorry it was my fault. My question is not clear.
I have code looks like this:
Try this:
StringBuilder sb = new StringBuilder();
for(i = 0; i < geo.getTargets().length ; i++ ){
if(geo.getTargets(i).getTargetType().equalsIgnoreCase("ProximityTarget")){
final ProximityTarget prox = (ProximityTarget)geo.getTargets(i);
float longitude = prox.getGeoPoint().getLatitudeInMicroDegrees());
float lat = prox.getGeoPoint().getLongitudeInMicroDegrees());
float radius = prox.getRadiusDistanceUnits();
if (sb.isEmpty()) {
sb.append("circle:" + longitude + ":" + lat + ":" + radius);
else {
sb.append(" | circle:" + longitude + ":" + lat + ":" + radius);
}
}
}
String result = sb.toString();
moreover, if it is possible to estimate the size of the concatenated string, you could use StringBuffer(int capacity)/StringBuilder(int capacity) to avoid resizing
This is the basic way of doing what you asked for with String +
operator.
String result = "";
for(i = 0; i < geo.getTargets().length ; i++ ){
if(geo.getTargets(i).getTargetType().equalsIgnoreCase("ProximityTarget")){
final ProximityTarget prox = (ProximityTarget)geo.getTargets(i);
float longitude = prox.getGeoPoint().getLatitudeInMicroDegrees());
float lat = prox.getGeoPoint().getLongitudeInMicroDegrees());
float radius = prox.getRadiusDistanceUnits();
if (!result.isEmpty()) {
result += "|";
}
result += ("circle:" + longitude + ":" + lat + ":" + radius);
}
}
return result;
public String asString(ProximityTarget target) {
StringBuilder sb = new StringBuilder("circle:");
sb.append(target.getGeoPoint().getLatitudeInMicroDegrees()).append(":");
sb.append(target.getGeoPoint().getLongitudeInMicroDegrees()).append(":");
sb.append(target.getRadiusDistanceUnits());
return sb.toString();
}
public void someMethod() {
//...
StringBuilder sb = new StringBuilder();
for(i = 0; i < geo.getTargets().length ; i++ )
{
if(geo.getTargets(i).getTargetType().equalsIgnoreCase("ProximityTarget"))
{
final ProximityTarget prox = (ProximityTarget)geo.getTargets(i);
if (!sb.isEmpty())
sb.append("|");
sb.append(asString(prox));
}
}
String formattedString = sb.toString();
//...
}
The most basic way of doing Java string concatenation is to use the +
operator. Set up:
String value = string1 + string2 + string3;
There are other ways to do it, but this simple case should handle your needs. For further information, look into StringBuilder
.
Depends on the kind of environment you're on. If you don't need thread safety, use stringbuilder. The stringbuffer class provides the necessary synchronisation when needed.