Attempt to Read an Array Index (5) That Is Out of Bounds (5). Please Check Length First.

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer'south Best Practices
  • Questions and Answers
  • Constructive Resume Writing
  • Hour Interview Questions
  • Reckoner Glossary
  • Who is Who

How to handle Java Array Index Out of Premises Exception?


More often than not, an array is of fixed size and each element is accessed using the indices. For instance, we have created an array with size ix. Then the valid expressions to access the elements of this array will exist a[0] to a[8] (length-1).

Whenever you used an –ve value or, the value greater than or equal to the size of the array, then the ArrayIndexOutOfBoundsException is thrown.

For Case, if you execute the following code, it displays the elements in the array asks you to requite the alphabetize to select an chemical element. Since the size of the array is 7, the valid index will be 0 to 6.

Example

import java.util.Arrays; import java.util.Scanner;  public course AIOBSample {    public static void main(String args[]) {       int[] myArray = {897, 56, 78, 90, 12, 123, 75};       System.out.println("Elements in the array are:: ");       System.out.println(Arrays.toString(myArray));       Scanner sc = new Scanner(System.in);       Arrangement.out.println("Enter the alphabetize of the required chemical element ::");       int element = sc.nextInt();       System.out.println("Chemical element in the given alphabetize is :: "+myArray[chemical element]);    } }

But if you find the below output we take requested the element with the index 9 since it is an invalid index an ArrayIndexOutOfBoundsException raised and the execution terminated.

Output

Elements in the assortment are:: [897, 56, 78, 90, 12, 123, 75] Enter the index of the required element :: vii Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at AIOBSample.main(AIOBSample.java:12)

Treatment the exception

You can handle this exception using try catch as shown below.

Instance

import java.util.Arrays; import java.util.Scanner;  public grade AIOBSampleHandled {    public static void main(Cord args[]) {       int[] myArray = {897, 56, 78, ninety, 12, 123, 75};       Arrangement.out.println("Elements in the array are:: ");       Organisation.out.println(Arrays.toString(myArray));       Scanner sc = new Scanner(System.in);       System.out.println("Enter the alphabetize of the required element ::");       endeavor {          int chemical element = sc.nextInt();          Organization.out.println("Element in the given index is :: "+myArray[element]);       } catch(ArrayIndexOutOfBoundsException due east) {          Organization.out.println("The alphabetize you have entered is invalid");          System.out.println("Delight enter an index number betwixt 0 and 6");       }    } }

Output

Elements in the array are:: [897, 56, 78, 90, 12, 123, 75] Enter the index of the required chemical element :: seven The index y'all have entered is invalid Please enter an index number betwixt 0 and 6

raja

Published on 09-Jan-2018 07:29:08

  • Related Questions & Answers
  • How to capture out of array alphabetize out of bounds exception in Coffee?
  • What is out of bounds index in an array - C language?
  • Accessing array out of bounds in C/C++
  • How to capture index out of range exception in C#?
  • Out of retention exception in Coffee:
  • How to handle the Runtime Exception in Coffee?
  • How to handle the exception using UncaughtExceptionHandler in Java?
  • How to handle an exception using lambda expression in Java?
  • How to handle an exception in JShell in Java 9?
  • How to handle an exception in Python?
  • How to handle Python exception in Threads?
  • How to handle an exception in JSP?
  • How to capture out of memory exception in C#?
  • Why accessing an array out of bounds does not give any fault in C++?
  • How to handle python exception inside if statement?

amadohimseoultz.blogspot.com

Source: https://www.tutorialspoint.com/How-to-handle-Java-Array-Index-Out-of-Bounds-Exception

0 Response to "Attempt to Read an Array Index (5) That Is Out of Bounds (5). Please Check Length First."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel