Signup/Sign In
LAST UPDATED: MARCH 5, 2021

Java Program To Check Array Bounds while Inputting Elements into an Array

In this tutorial, we will learn how to check array bounds while inputting elements into an array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input:

Enter Array Size: 5

Enter Array Elements: 4 6 2 3 7 8

Output: Array Bounds Exceeded...

Program 1: To Check Array Bounds while Inputting Elements into an Array

In this approach, we will see how to check array bounds while inputting the elements of an array using a try catch block. The logic behind using this approach is that firstly we declare an array of size n. Then, we ask the user to give the input. Here, we take the input using a try block. But we ask the user to give inputs more than the size of the array that is we ask the user to input n+1 elements. Since we have declared the array of size n but we are giving n+1 elements as input, so ArrayIndexOutOfBoundsException is thrown. This exception is now handled by the catch block. So the user will now get an output that the number of elements declared is more than the array size. So try again.

Algorithm

  1. Start.
  2. Declare the array size.
  3. Ask the user to initialize the array size.
  4. Declare the array.
  5. Take a Try-Catch Block.
  6. Inside the Try Block, ask the user to initialize the array elements.
  7. Enter the elements more than the specified size.
  8. This will throw the ArrayIndexOutOfBoundsException.
  9. Now the catch block will print the message Array Bounds Exceeded... Try Again.
  10. Stop

Below is the code for the same.

The below program demonstrates how to check array bounds while inputting elements into an array using Try Catch Block.

/*Java Program to Check Array Bounds while Inputing elements into an Array*/
import java.util.*; 
  
public class Main 
{ 
    // Main driver method 
    public static void main(String args[]) 
        throws ArrayIndexOutOfBoundsException 
    { 
  
        // Taking input from user 
        Scanner sc = new Scanner(System.in); 
        
        //Ask the user to enter the Array Size
        int n;
        System.out.println("Enter the Array Size ");
        n=sc.nextInt();
        
        // Storing user input elements in an array 
        int arr[] = new int[n]; 
        
        System.out.println("Enter more number of elements than the mentioned size ");
        
        // Try block to check exception 
        try { 
            // Forcefully iteration loop no of times 
     
            for (int i = 0; i <= n; i++) 
            { 
                arr[i] = sc.nextInt(); 
            } 
        } 
        catch (ArrayIndexOutOfBoundsException e) 
        { 
            // Print message when any exception occurs 
            System.out.println("Array Bounds Exceeded..."); 
            System.out.println("Try Again"); 
        } 
    } 
}


Enter the Array Size 3
Enter more number of elements than the mentioned size
3 5 2 1
Array Bounds Exceeded...
Try Again

Program 2: To Check Array Bounds while Inputting Elements into an Array

In this approach, we will limit the number of inputs using a while loop. This is the easiest method of checking array bounds while taking inputs from the user. The logic behind using this approach is that we are restricting the number of inputs given by the user using a while loop. Once the loop variable matches the array size, the loop will stop taking inputs and will display the array.

Algorithm

  1. Start
  2. Declare the array size.
  3. Ask the user to initialize the array size.
  4. Declare the array.
  5. Ask the user to initialize the array elements.
  6. Take a try-catch block.
  7. Inside the try block, ask the user to initialize the array elements.
  8. Use a while loop for the same.
  9. Enter the elements more than the specified size.
  10. This will throw the ArrayIndexOutOfBoundsException.
  11. Now the catch block will print the message Array Bounds Exceeded... Try Again.
  12. Stop

Below is the code for the same.

The below program demonstrates how to check array bounds while inputting elements into an array by limiting the input using a while loop.

/*Java Program to Check Array Bounds while Inputing elements into an Array*/
import java.util.*; 
  
public class Main 
{ 
    // Main driver method 
    public static void main(String args[]) 
    { 
  
        // Taking input from user 
        Scanner sc = new Scanner(System.in); 
        
        //Ask the user to enter the Array Size
        int n;
        System.out.println("Enter the Array Size ");
        n=sc.nextInt();
        
        // Storing user input elements in an array 
        int arr[] = new int[n]; 
        
        int i = 0; 
        
        System.out.println("Enter the Array elements : "); 
        try{
        // Condition check 
            while (true) 
            { 
                if (i == n+1) 
  
                    // Statement to throw an exception 
                    throw new ArrayIndexOutOfBoundsException(); 
                arr[i++] = sc.nextInt(); 
            } 
        
        }
         
        catch (ArrayIndexOutOfBoundsException e) 
        { 
            // Print message when any exception occurs 
            System.out.println("Array Bounds Exceeded..."); 
            System.out.println("Try Again"); 
        } 

    } 
}


Enter the Array Size 5
Enter the Array elements : 7 9 5 6 4 3
Array Bounds Exceeded...
Try Again



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.