title: java - How to handle array:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
body: <p>In my code i am comparing the 2 elements of an array. but i got the following exception.please can anybody help me</p>
  
  <blockquote>
    <p>array:Exception in thread "main"
    java.lang.ArrayIndexOutOfBoundsException: 3   at
    Sarray.main(Sarray.java:64)</p>
  </blockquote>
  
  <p><br></p>
  
  <pre><code>public class Sarray    
  {    
          public static void main(String[] args)  
         {     
  
  
  
       Scanner scan = new Scanner(System.in); 
       System.out.print( "Enter sorted array length:" );
       int length = scan.nextInt();
  
       int[] a = new int[length];
       System.out.println("Enter integer sorted array:");      
  
       for(int i = 0;i&lt;length;i++) 
       {
           String token = scan.next();
           a[i] = Integer.parseInt(token);         
       }       
       System.out.print("Unique array:");
       int[] b=new int[length];        
       int k=0;
       for(int i=0;i&lt;length;i++)
       {  
          //here i got Exception              
           if(a[i] != a[i+1])
           {
               b[k++]= a[i];
           }
       }               
       for(int i=0;i&lt;k;i++)
       {
           System.out.print(b[i]+" "); 
       }   
   }
  }
  </code></pre>