JetBrains Academy: Array exceptions - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Array exceptions

Out of bounds!:

Take a look at the program that reads a string and a number N and outputs the Nth element of a string (starting from 0).

import java.util.*;

class FixingStringIndexOutOfBoundsException {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String string = scanner.nextLine();
        int index = scanner.nextInt();

        System.out.println(string.charAt(index));
    }
}

This program may throw StringIndexOutOfBoundsException. Fix it to avoid the exception.

In the case when the exception might happen, your updated program should output: "Out of bounds!".

import java.util.*;

class FixingStringIndexOutOfBoundsException {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String string = scanner.nextLine();
        int index = scanner.nextInt();

        if (index < 0 || index >= string.length()) {
            System.out.println("Out of bounds!");
        } else {
            System.out.println(string.charAt(index));
        }
    }
}