1

Im building a program and it has to take a set of information (Runners and their times) and make a method that will compare the times of all runners. Im a beginner and don't know much and have to stick to what I know. I need to get the lowest runners in the 2017 year please lead me in the right direction. (I want to learn but not get the answer!!!) Thanks!

Ive tried to compare all the integers of the array but that seemed long and also wrong

public static void main(String[] args) {
    int runnersTime2017 [] = {357, 299, 432, 326, 275, 450, 265, 343, 264, 308, 242, 377, 273};
    int runnersTime2018 [] = {341, 307, 328, 283, 274, 359, 256, 323, 269, 308, 249, 340, 238};
    fastestRunner2017(runnersTime2017);


 }

public static int fastestRunner2017(int Array1 []){ //This is what I am trying to do


    if (Array1[1] <= Array1[]){

        System.out.println(T1);

        return T1;

    }
    //This is what I did before and seemed wrong
    else if (T2 <= T1 && T2 <= T3 && T2 <= T4 && T2 <= T5 && T2 <= T6 && T2 <= T7 && T2 <= T8 && T2 <= T9 && T2 <= T10 && T2 <= T11 && T2 <= T12 && T2 <= T13){

        System.out.println(T2); 

        return T2;
    }    

    else if (T3 <= T1 && T3 <= T2 && T3 <= T4 && T3 <= T5 && T3 <= T6 && T3 <= T7 && T3 <= T8 && T3 <= T9 && T3 <= T10 && T3 <= T11 && T3 <= T12 && T3 <= T13){

        System.out.println(T3);

        return T3;
    }

    else if (T4 <= T1 && T4 <= T2 && T4 <= T3 && T4 <= T5){
        System.out.println(T4);

        return T4;
    }

    else {

        System.out.println(T13);
        return T13;

    }

I want to compare one part of the array to the rest of them

Nhami
  • 15
  • 6
  • What's the expected output? The fastest runner of 2017? Or the difference between 2017/2018 etc – Mark Apr 17 '19 at 18:33
  • 4
    Possible duplicate of [Finding the max/min value in an array of primitives using Java](https://stackoverflow.com/questions/1484347/finding-the-max-min-value-in-an-array-of-primitives-using-java) – Joakim Danielson Apr 17 '19 at 18:35
  • This is definitely a duplicate to that @JoakimDanielson but most of those answers are likely too advanced for an intro developer – Matt Apr 17 '19 at 18:36
  • I need to get the fastest runner of 2017 and the names – Nhami Apr 17 '19 at 18:38
  • 1
    What research have you done, have you tried to search on-line for possible solutions? – Joakim Danielson Apr 17 '19 at 18:41
  • @JoakimDanielson yeah tried a few sites and they didn't really help as they were using too much code that I had not learned yet. I do want to figure this program on my own but need some guidance to get to the answer that's it – Nhami Apr 17 '19 at 18:43
  • 1
    Some more possible duplicate with hopefully more suitable answers https://stackoverflow.com/questions/18828091/how-to-get-the-minimum-maximum-value-of-an-array, https://stackoverflow.com/questions/47383316/finding-a-minimum-and-maximum-value-in-an-array, https://stackoverflow.com/questions/37820307/min-and-max-value-of-an-array-in-java – Joakim Danielson Apr 17 '19 at 18:45
  • @JoakimDanielson Thanks will check them out and get back to you – Nhami Apr 17 '19 at 18:45
  • Possible duplicate of [how to get the minimum,maximum value of an array?](https://stackoverflow.com/questions/18828091/how-to-get-the-minimum-maximum-value-of-an-array) – Matt Apr 17 '19 at 18:47

2 Answers2

1

The most straight-forward way to solve this would be a for-loop.

If I'm understanding your case here, you would take each value of the first array and compare it to each value of the second array. If that first array value is greater than each, print it. Otherwise take the greater value. Something like:

foundValue = runnersTime2017[0];
for (int i = 0; i < runnersTime2017.length; ++i) {
    for (int j = 0; j < runnersTime2018.length; ++j) {
        if (runnersTime2018[j] > runnersTime2017[i]) {
            foundValue = runnersTime2018[j];
         }
    }
}
return foundValue;

Note that this is also a relatively inefficient method (O(n^2)), but for a beginner it should suffice without going over your head.

Kemilio
  • 88
  • 8
  • yeah that's pretty close to what I need but how would I pull out a specific number like if I need the top 3 runners – Nhami Apr 17 '19 at 18:50
  • Then you would need three "foundValue" variables to keep track of each. In addition, you'll need more logic to compare each of the three "foundValues" to each new array value you comparing against. The exact mechanics of that I'll leave up to you, but if I were you I'd start with the three first values in the first array, create a function that compares the least of those values to each new value being compared against and, if the new value is greater than the least of the three, check it against the other two. If you need to keep track of many, maybe keeping a new array would be a good idea. – Kemilio Apr 17 '19 at 18:56
0

How about sort the Int array and take the first or last value

     int runnersTime2017 [] = {357, 299, 432, 326, 275, 450, 265, 343, 264, 308, 242, 377, 273};

        int runnersTime2018 [] = {341, 307, 328, 283, 274, 359, 256, 323, 269, 308, 249, 340, 238};

       List<Integer> allRunner= new ArrayList<>(runnersTime2017.length+runnersTime2018.length);

     for(int r7 :runnersTime2017) {
         allRunner.add(r7);

     }
     Collections.sort(allRunner);
     //this prints the 2017 runners after storting
     System.out.println(allRunner);
     for(int r8 :runnersTime2017) {
         allRunner.add(r8);
     }
     //now sort again after adding 2018 runners  
     Collections.sort(allRunner);
     System.out.println(allRunner);
tsingh
  • 361
  • 1
  • 5
  • 17
  • Thanks but some of that code I'm unfamiliar with is there another way to sort the numbers – Nhami Apr 17 '19 at 18:59
  • Java has builtin in sorting algorithms for various collection types.It does't get much simpler than Collections.sort(allRunner).I am not sure whats the confusion? – tsingh Apr 17 '19 at 19:46