Java Arena
Step 4 of 8

When the index doesn't exist

A list with three values has the indexes 0, 1 and 2, and no others. Asking for any other index stops the program with an error:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> chores = new ArrayList<>();
        chores.add("do the dishes");
        chores.add("water the plants");
        chores.add("take out the trash");
        System.out.println(chores.get(0));
        System.out.println(chores.get(3));
        System.out.println("All done");
    }
}
Output
do the dishes
The program crashes with
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
	at Main.main(Main.java:10)

The program compiled without a complaint: javac can't know how many values a list will hold when the program runs. The error comes while it runs, as an exception, and the program stops on the spot. That's why All done is never printed. Java's message says what went wrong and where:

  • IndexOutOfBoundsException is the kind of error: an index outside the list.
  • Index 3 out of bounds for length 3 gives the index that was asked for, and how many values the list had.
  • at Main.main(Main.java:10) is where it happened: line 10, in the method main.

In a real run, Java also prints a few at lines from inside its own classes, where the index was checked. The one to look for is the at Main line: that's your code.

A negative index crashes the same way (Index -1 out of bounds for length 3), and so does every index on an empty list (Index 0 out of bounds for length 0).

Avoiding it

The largest index is always one less than the number of values. A common cause of this crash is counting from 1, as people do, instead of from 0: get(3) for the third value, or a loop that runs one round too far. When the index in the message equals the length, as in Index 3 out of bounds for length 3, look for exactly that mistake.

The next step shows how to ask a list how many values it holds, so a program can check an index before it uses it.

Your task

Challenge 1 of 3

This program crashes. Run it, read the message, and fix the mistake, so the program prints:

First stop: Station
Second stop: Library
Last stop: Harbor

Challenge 2

The cafeteria's menu has a dish for each index from 0 to 4. The program reads an index and prints that dish, but it crashes for an index outside the menu. Add a check: for an index from 0 to 4, print the dish as before; for any other index, print No dish at index and the index. For the input 7 the program prints:

No dish at index 7

For the input 2 it prints Dish: curry.

Challenge 3

This program prints four lap times and then crashes before Race over. Read the message to find the cause, and fix it, so the program prints:

Lap 1: 62 s
Lap 2: 58 s
Lap 3: 60 s
Lap 4: 57 s
Race over