Java Program to Check Array Bounds while inputting Elements into the Array



Array is a linear data structure that is used to store a group of elements with similar data types. It stores data in a sequential manner. Once we create an array, we can't change its size, i.e., it is of fixed length.

This article will help you to understand the basic concept of arrays and array bounds in Java. Also, we will discuss Java programs to check array bounds while inputting elements into the array.

Array and Array Bound

We can access elements of an array by its index. Suppose we have an array of length N, then its indices will be from 0 to N-1. Let's see it with an example diagram:

We can see in the above diagram that there are 7 elements in the array, but the index value is from 0 to 6, i.e., 0 to 7-1.

The range of the array is called its bound. The range of the above array is from 0 to 6; therefore, we can also say that 0 to 6 is the bound of the given array.

If we try to access the index value out of its range or a negative index, then we will get an ArrayIndexOutOfBoundsException. It is an error that occurs at runtime.

Checking Array Bounds in Java

If we access the elements of an array within its bounds, i.e., if the index of the element we access is less than length-1, then we won't get any error.

The program will be successfully executed. However, if we try to access the index value of an array out of its range or a negative index, then we will get a runtime error.

Therefore, it is necessary to find the number of elements the array holds before accessing its values or inputting elements into it. You can check the array length using its length property. It is a final variable that is directly accessible through the array objects.

Example: ArrayIndexOutOfBoundsException

Let's try to print the value outside the range of the given array.

public class Tutorialspoint {
      public static void main(String []args) {
      String[] item = new String[5];
      item[0] = "Rice";
      item[1] = "Milk";
      item[2] = "Bread";
      item[3] = "Butter";
      item[4] = "Peanut";
      // trying to run the for loop till index 5
      for(int i = 0; i <= 5; i++) {
         System.out.println(item[i]);
      }
   }
}

Output:

In the above program, we have tried to execute the for loop till index 5 of the array, but its range is 0 to 4 only. Therefore, after printing the elements till 4, we will get an error as shown below:

Rice
Milk
Bread
Butter
Peanut
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at Tutorialspoint.main(Tutorialspoint.java:11)

Example: Checking Array Bound

In the following Java program, we will use the length property to check the number of elements of the array before accessing it.

public class Main {
   public static void main(String []args) {
      // declaration and initialization of array 
      String[] item = new String[5]; 
      // 0 to 4 is the indices 
      item[0] = "Rice";
      item[1] = "Milk";
      item[2] = "Bread";
      item[3] = "Butter";
      item[4] = "Peanut";
      System.out.print(" Elements of the array item: " );
      // loop will iterate till 4 and will print the elements 
      for(int i = 0; i < item.length; i++) {
         System.out.print(item[i] + " ");
      }
   }
}

The program will run successfully without any error:

Elements of the array item: Rice Milk Bread Butter Peanut

Handling the Array Bound Error

You can handle the ArrayIndexOutOfBoundsException either by using try and catch blocks or an if-else block.

Example

In this example, we use the try and catch blocks to check and handle the array bound error while inputting the elements into it.

import java.util.*;
public class Tutorialspoint {
   public static void main(String []args) throws ArrayIndexOutOfBoundsException {
      // Here ?sc' is the object of scanner class
      Scanner sc = new Scanner(System.in); 
      System.out.print("Enter number of items: ");
      int n = sc.nextInt();
      // declaration and initialization of array ?item[]'
      String[] item = new String[n]; 
      // try block to test the error
      try {
         // to take input from user
         for(int i =0; i<= item.length; i++) {
            item[i] = sc.nextLine();
         }
      }
      // We will handle the exception in catch block
      catch (ArrayIndexOutOfBoundsException exp) {
         // Printing this message to let user know that array bound exceeded
         System.out.println(
         " Array Bounds Exceeded  \n Can't take more inputs ");
      }
   }
}

Following is the output of the above program -

Enter number of items: 3
3
5
7
Array Bounds Exceeded  
 Can't take more inputs
Updated on: 2025-06-19T14:41:38+05:30

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements