Sopieee
Sopieee

Reputation: 21

How do you return multiple values based on an if statement in a String type method (java)?

I am working on a part of an assignment that requires me to the compute runners who ran above the average speed (based on 15 runners whose speed/names.. are given in the assignment and I already added all 15 values to the array in the main method).

In the definition class, I have created a method ( which must be type String according to assignment) to compute if runner [i]'s time is =< average speed.

This is my method for finding the above average runners (there are more than one) BUT this method only prints ONE runner when ran in main. How would I allow for MULTIPLE array values to be returned??

Code:

// Finding above average:

public static String getAboveAverageRunners(BanffMarathonRunner[] runners) {

    for (int i = 0; i < runners.length; i++) {

        if (runners[i].getTime() <= BanffMarathonRunner.getAverageTime(runners)) {

            String aboveAverage = runners[i].getFirstName() + " " + runners[i].getLastName() + ", Years Competing: "
                    + runners[i].getYears();

            return aboveAverage;

        } // End of if

    } // End of for loop

    return null;

} // End of getAboveAverageRunner method:


} // End of class BanffMarathonRunner:

Revised :

public static String getAboveAverageRunners(BanffMarathonRunner[] runners) {

    ArrayList<String> thoseAbove = new ArrayList <String>();

    for (int i = 0; i < runners.length; i++) {

        if (runners[i].getTime() <= BanffMarathonRunner.getAverageTime(runners)) {

            String aboveAverage = runners[i].getFirstName() + " " + runners[i].getLastName() + ", Years Competing: "
                    + runners[i].getYears();


            thoseAbove.add(aboveAverage);

            //return aboveAverage;

            // End of if statement:
        }

        // End of for loop:
    }
    return thoseAbove;

Now, I get an error on the return thoseAbove

Type mismatch: cannot convert from ArrayList<String> to String

I understand the error I just don't know how to fix it while keeping the return type.

Upvotes: 0

Views: 674

Answers (2)

Bohemian
Bohemian

Reputation: 425208

Your problem is you're stopping at the first runner that qualifies and returning that. Instead you need to somehow collect all runners that qualify.

You probably want to return a List<String> rather than a String. Fortunately, java 8 can help here:

public static List<String> getAboveAverageRunners(BanffMarathonRunner[] runners) {
    int averageTime = BanffMarathonRunner.getAverageTime(runners);

    return Arrays.stream(runners).filter(r -> r.getTime() < average)
      .map(r -> r.getFirstName() + " " + r.getLastName() + ", Years Competing: " + r.getYears())
      .collect(Collectors.toList());
}

If you absolutely must return a single String value (not recommended), then join them with semi-colons (or whatever you like):

public static List<String> getAboveAverageRunners(BanffMarathonRunner[] runners) {
    int averageTime = BanffMarathonRunner.getAverageTime(runners);

    return Arrays.stream(runners).filter(r -> r.getTime() < average)
      .map(r -> r.getFirstName() + " " + r.getLastName() + ", Years Competing: " + r.getYears())
     .collect(Collectors.joining(",", "\"", "\""));
}

Note: this will return a csv of quoted values, like "John Smith ...", "Bob Brown ...", .... The quotes are added in case the data contains a comma.

Better yet would be to return a List<Runner>, and render them elsewhere as you need:

public static List<Runner> getAboveAverageRunners(BanffMarathonRunner[] runners) {
    int averageTime = BanffMarathonRunner.getAverageTime(runners);

    return Arrays.stream(runners).filter(r -> r.getTime() < average)
      .collect(Collectors.toList());
}

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44854

If it has to return a string then how about returning a pipe delimitered string?

e.g.

StringBuilder myList = new StringBuilder ();
for (int i = 0; i < runners.length; i++) {

    if (runners[i].getTime() <= BanffMarathonRunner.getAverageTime(runners)) {

        String aboveAverage = runners[i].getFirstName() + " " + runners[i].getLastName() + ", Years Competing: "
                + runners[i].getYears();

        myList.append (aboveAverage).append ("|");

        // End of if statement:
    }

    // End of for loop:
}
return myList.toString ();  // maybe remove the trailing pipe

Then in your calling method you can use String.split ("\\|")

Upvotes: 3

Related Questions