Java Arena
Step 8 of 8

Searching a list

Is a value in the list, and where? Go through the indexes and compare each value with the one you're looking for. Keep the index of the match in a variable that starts at -1: no real index is -1, so it means "not found yet".

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> shelf = new ArrayList<>();
        shelf.add("atlas");
        shelf.add("cookbook");
        shelf.add("diary");
        shelf.add("novel");

        String wanted = scanner.nextLine();
        int found = -1;
        for (int i = 0; i < shelf.size(); i++) {
            if (shelf.get(i).equals(wanted)) {
                found = i;
                break;
            }
        }

        if (found == -1) {
            System.out.println("No " + wanted + " on the shelf");
        } else {
            System.out.println("The " + wanted + " is at index " + found);
        }
    }
}
Input
diary
Output
The diary is at index 2
  • shelf.get(i) is a String, so it's compared with equals, as always for Strings.
  • break stops the search at the first match. Without it, the loop would go on, and found would end up as the index of the last match.
  • Only after the loop is it certain that the value isn't there: found is still -1.

For numbers, keep the value you're looking for in an int variable, and numbers.get(i) == wanted compares the numbers.

Every match

To find all the places a value appears, leave out the break, and act on each match inside the loop. Here each six is printed and counted:

ArrayList<Integer> rolls = new ArrayList<>();
rolls.add(3);
rolls.add(6);
rolls.add(2);
rolls.add(6);
rolls.add(6);
rolls.add(1);

int sixes = 0;
for (int i = 0; i < rolls.size(); i++) {
    if (rolls.get(i) == 6) {
        System.out.println("Six on roll " + (i + 1));
        sixes++;
    }
}
System.out.println("Sixes: " + sixes);
Output
Six on roll 2
Six on roll 4
Six on roll 5
Sixes: 3

Lists have ready-made methods for some searches, such as indexOf and contains, and the next module introduces them. Writing the loop yourself shows how they work, and a loop can do more than they can, like finding every match.

Your task

Challenge 1 of 3

A music player repeats its playlist: after the last song, it starts again from the first. Read the song titles, one per line, until end. Then read one more title: the song playing now. Print Up next: and the song that comes after it, or Unknown song when the title isn't in the playlist. When the song is in the playlist twice, go by its first place. For the input Night Drive, Low Tide, Glass Garden, end and Low Tide the program prints:

Up next: Glass Garden

After Glass Garden, the next song would be Night Drive.

Challenge 2

Read the weather of each day, one word per line (sun, rain or cloud), until end. Keep the days in a list. Then print Rain on day and the day's number (counting from 1) for every rainy day, and after them Rainy days: and how many there were. For the input sun, rain, cloud, sun, rain and end the program prints:

Rain on day 2
Rain on day 5
Rainy days: 2

Challenge 3

Time to put the module together. Read a runner's lap times in whole seconds, one per line, until 0. There's at least one lap. Then print:

  • Laps: and the number of laps,
  • Average: and the average lap time, as a decimal number,
  • Fastest: lap and the number of the fastest lap, counting from 1 (on a tie, the earlier lap),
  • Slower than average:, and after it lap and the number of every lap whose time is above the average, one per line.

For the input 64, 61, 58, 60, 63 and 0 the program prints:

Laps: 5
Average: 61.2
Fastest: lap 3
Slower than average:
lap 1
lap 5