0

I have two arrays, one is a String array full of marathon runners and the next is an Integer array full of the marathon runners' respective times. I'm trying to output the fastest runner + his/her time as well as the second fastest runner + his/her time. So far, I've been able to output the fastest runner and his time, but when I try to output the second fastest runner + her time, the loop outputs two runners' times instead of one. I've attached the code for reference:

Also, any comments in terms of simplifying/improving the code is also welcome.

public class APCS {
  public static void main(String[] arguments) {

    String[] names = {
        "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
        "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",
        "Aaron", "Kate"
    };

    int[] times = {
        341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,
        343, 317, 265
    };

    int timeIndex = 0;
    int secondTimeIndex = 0;
    for (int i = 0; i < times.length; i++) {
        if (times[i] > times[timeIndex]) {
            timeIndex = i;
            System.out.println(names[timeIndex] + " " + times[timeIndex]);
        }

        if (times[i] > times[secondTimeIndex]) {
            if (times[i] == times[timeIndex]) {
                continue;
            }
            secondTimeIndex = i;
            System.out.println(names[secondTimeIndex] + " " + times[secondTimeIndex]);
        }
    }

  }
}

This is my output:

Phil 445
Matt 402
Jane 412
4
  • 1
    I would consider using a Map - that might simplify your code. SortedMap might be very useful for you. Commented Aug 10, 2015 at 21:03
  • 3
    @James: or a class Runner implements Comparable<Runner> type class that holds String name and int time fields, and then sort the array or List. Or better still, use a Comparator<Runner>. Commented Aug 10, 2015 at 21:04
  • CS101: Since you're outputting a fixed amount of data (exactly 2, not data-per-person), the output statements need to be outside the loop. Commented Aug 10, 2015 at 21:10
  • You are printing out the fast running found so far, you should be be printing out the fastest running over all, after the loop has run. Commented Aug 10, 2015 at 21:13

2 Answers 2

4

Your System.out.println is in the wrong place and now is reporting changes in the desired indices, not the final results. The printlns should be called after the for loop has completed.

for (int i = 0; i < times.length; i++) {
    if (times[i] > times[timeIndex]) {
        timeIndex = i;
        //System.out.println(names[timeIndex] + " " + times[timeIndex]);
    }

    if (times[i] > times[secondTimeIndex]) {
        if (times[i] == times[timeIndex]) {
            continue;
        }
        secondTimeIndex = i;
        //System.out.println(names[secondTimeIndex] + " " + times[secondTimeIndex]);
    }
}
System.out.println("Fastest: " + names[timeIndex] + " " + times[timeIndex]);
System.out.println("Second: " + names[secondTimeIndex] + " " + times[secondTimeIndex]);
Sign up to request clarification or add additional context in comments.

1 Comment

This code looks like it has 2 bugs to me that were part of the original code. First, the reqs are looking for fastest time (according to the question), which should be the lowest time, not the greatest, right? Second, if the second fastest time appears before the fastest time in the list, then it will be skipped over and never have its index registered into secondTimeIndex.
1

Because you're writing this in Java, you shouldn't be using parallel arrays. Java is specifically designed to be object oriented, so I suggest using objects to represent the marathon runners.

For example, you can declare a Runner class as such:

public class Runner implements Comparable<Runner> {
    private String name = null;
    private int time = 0;

    public Runner(String name, int time) {
        this.name = name;
        this.time = time;
    }

    public String toString() {
        return name + " " + time;
    }

    public int compareTo(Runner r) {
        return (r.time - time);
    }
}

compareTo is implemented so you can sort the list of runners.

Then you create the list of runners and sort them with the Arrays class.

Runner[] runners = new Runner[16];
runners[0] = new Runner("Elena", 341); //and so on, creating all runners

Arrays.sort(runners); //runners will now be sorted according to times
System.out.println(runners[i].toString()); //first place
System.out.println(runners[1].toString()); //second place

2 Comments

System.out.println(runners[i].toString()) needs fixing
What do you mean by "needs fixing"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.