Shrey
Shrey

Reputation: 532

Java – For loop isn't properly executing through an array

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

Upvotes: 0

Views: 124

Answers (2)

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

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285440

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]);

Upvotes: 4

Related Questions