A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

ArrayIndexOutOfBoundsException in java

//import Scanner class
import java.util.Scanner;
class ArrayIndexOutOfBoundsExc
{
    // define main method from where execution of program starts
    public static void main(String args[])
    {
        //define array in java
        int a[]=new int[5]; 
        //create object of scanner class
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter elements: ");
        // Handling array index
        try
        {
            for(int i=0; i<=7; i++)
            {
                a[i]=sc.nextInt();
            }
        }
        catch(Exception e)
        {
            System.out.println("We got array index of bounds exception.");
        }
        // if exception handled then you can continue rest of program
        System.out.println("Enter your name: ");
        String name=sc.next();
    }
   }

LEARN TUTORIALS

.

.