This exercise is asking us to make a "RoadTrip" class that creates an ArrayList of geolocations using a geolocation class. Part of the exercise asks that we make a toString method within the RoadTrip Class that would end up returning a string like:
1. San Francisco (37.7833, -122.41671)
2. Los Angeles (34.052235, -118.2436831)
3. Las Vegas (36.114647, -115.1728131)
making a string for each of the GeoLocation objects within the ArrayList.
But I cannot put the return statement in a for loop. Here's an example of me "cheating" to get it do simulate what I would want it do actually do.
public String toString()
{
int counter = 1;
for (int i = 0; i < locationList.size() ; i++)
{
System.out.println(counter + ". " + locationList.get(i).toString());
counter++;
}
return "";
}
If I were to simply replace the System.out.println() with return and remove the return "";, I would get the errors:
RoadTrip.java:43: error: unreachable statement
counter++;
^
RoadTrip.java:45: error: missing return statement
}
^
2 errors
I saw other solutions that would utilize a StringBuilder, but I am assuming that the creators of the curriculum intend that we complete the exercises with the tools we are provided. Is there another method that I can use that would limit itself to the given "toolset"?
Pardon me if my techincal language is off, I'm still relatively new to coding.