Out of bounds!

Report a typo

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).

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

Tip: Do not forget about negative numbers!

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

Write a program in Java 17
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));
}
}
___

Create a free account to access the full topic