2

I'm making a program to determine who has the fastest time in a marathon out of an array of times and one of corresponding names. In order to make them match up I return the index of the lowest time instead of the value of the index. When I return the value of the index instead of the index it returns the correct name and time, however, when returning the index it returns the last value in both arrays.

package Lec3;

public class Marathon 
{
    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
        };

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

        int key = firstPlace(times, names);

        System.out.println("In first place is " + names[key] + " with a time of " + times[key] + " minutes!");

    }

    public static int firstPlace(int[] time, String[] names)
    {
        int i;
        int bestTime = 1000;
        int firstValue = 0;

        for(i = 0; i < time.length; i++)
        {
            if(time[i] < bestTime)
            {
                firstValue = i; 
            }
        }
        return firstValue;
    }
}
1
  • better solution -> use hash Commented May 17, 2018 at 1:12

2 Answers 2

2
for(i = 0; i < time.length; i++)
{
    if(time[i] < bestTime)
    {
        firstValue = i; 
    }
}

In this loop you don't update bestTime so the times are all compared against the initial value of 1000. They're all smaller leading to a victory for the last one.

Sign up to request clarification or add additional context in comments.

1 Comment

Edit: I understand what you mean now in that I don't update bestTime and am only comparing it against the initial value. Thank you for your help!
2

You have a loop in main where you iterate names to display all of the names and times. You can find the lowest key in that loop. Like,

int key = 0;
for (int i = 0; i < names.length; i++) {
    System.out.println(names[i] + ": " + times[i]);
    if (times[i] < times[key]) {
        key = i;
    }
}
System.out.println("In first place is " + names[key] + " with a time of " + times[key] + " minutes!");

Or, in your current method, change

if(time[i] < bestTime)

to

if (time[i] < time[firstValue])

and eliminate bestTime.

1 Comment

@nox Get out a pencil and paper and write down what's happening every loop iteration: play computer.

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.