简体   繁体   中英

CodeHS: How use one class's toString method in another class's toString multiple times

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.

Why the problem happens-

  1. The control encounters the return statement on the first loop iteration and goes back to where the method was called from. Hence the following lines in the loop body are not reachable.

  2. Since the return statement is within a loop and is subject to conditional execution, the compiler tells you there is a missing return statement. See code below:

public class Program
{
    public static void main(String[] args) {
        System.out.println(method());
    }
    static int method()
    {
        int i= (int)Math.random();
        if(i>0)
            return 1;
    }
}

Since this is your assignment I won't be providing working code.

The easiest solution would be to define a String variable, store an empty String ( "" ) in it, concat whatever you need in the loop and return it.

If you cannot use StringBuilder, why not concatenate Strings like this;

public String toString() 
    {
        int counter = 1;
        String str = "";
        for (int i = 0; i < locationList.size() ; i++) 
        {
            str = str + counter + ". " + locationList.get(i).toString();
            str = str + "\n";
            counter++;
        }
        return str;
    }

PS - I didn't run the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM