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;
}
}