Java Arena
Step 4 of 6

Searching an array

Searching an array works just like searching a list with a loop: go through the indexes, and look at each element. This program reads a week of midday temperatures and does the three searches that come up most often:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] temperatures = new int[7];
        for (int i = 0; i < temperatures.length; i++) {
            temperatures[i] = Integer.valueOf(scanner.nextLine());
        }

        int firstFrost = -1;
        for (int i = 0; i < temperatures.length; i++) {
            if (temperatures[i] < 0) {
                firstFrost = i;
                break;
            }
        }
        if (firstFrost == -1) {
            System.out.println("No frost this week");
        } else {
            System.out.println("First frost on day " + firstFrost);
        }

        int warmDays = 0;
        for (int temperature : temperatures) {
            if (temperature >= 10) {
                warmDays++;
            }
        }
        System.out.println("Warm days: " + warmDays);

        int warmest = temperatures[0];
        int warmestDay = 0;
        for (int i = 1; i < temperatures.length; i++) {
            if (temperatures[i] > warmest) {
                warmest = temperatures[i];
                warmestDay = i;
            }
        }
        System.out.println("Warmest: " + warmest + " on day " + warmestDay);
    }
}
Input
8
12
3
-2
11
-4
5
Output
First frost on day 3
Warm days: 2
Warmest: 12 on day 1
  • Finding an index. firstFrost starts at -1, which is never an index, so it means "not found yet". At the first match, the loop stores the index and leaves with break. If nothing matched, it's still -1 after the loop.
  • Counting. A counter and an if. The index isn't needed, so a for-each loop is enough.
  • The largest value and its index. Start from the first element, not from 0: in a week where every day is below zero, 0 would beat them all, and the answer would be a temperature that never happened. Keep the index next to the value, and update both together.

With >, the first of two equal values wins, because a later one isn't bigger. With >=, the last one would win.

Your task

Challenge 1 of 3

The program reads the five jumps of a long jump contest, in centimeters. Fill in the blanks, so it finds the longest jump and the round it was in (the round is the index plus one). For the input 540, 575, 612, 598 and 602 it prints:

Best jump: 612 cm, in round 3

Challenge 2

The program reads the number of students in a class, then each student's locker number: student 1's first, then student 2's, and so on. Then it reads one more locker number. Search the array for it, and print which student has that locker (the index plus one), or that the locker is free. For the input 4, 17, 5, 42, 8 and then 42 the program prints:

Locker 42 belongs to student 3

For the locker 99 it would print Locker 99 is free.

Challenge 3

Read a count, then that many whole numbers. Print their average, and then how many of the numbers are above the average. The numbers must be kept in an array: the average is known only after the last one is read, and only then can they be compared with it. For the input 4, 2, 9, 4 and 7 the program prints:

Average: 5.5
Above average: 2