Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:58:34+00:00 2026-05-26T13:58:34+00:00

In my code i am comparing the 2 elements of an array. but i

  • 0

In my code i am comparing the 2 elements of an array. but i got the following exception.please can anybody help me

array:Exception in thread “main”
java.lang.ArrayIndexOutOfBoundsException: 3 at
Sarray.main(Sarray.java:64)

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<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<length;i++)
     {  
        //here i got Exception              
         if(a[i] != a[i+1])
         {
             b[k++]= a[i];
         }
     }               
     for(int i=0;i<k;i++)
     {
         System.out.print(b[i]+" "); 
     }   
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:58:35+00:00Added an answer on May 26, 2026 at 1:58 pm
     for(int i=0;i<length;i++)     
    

    should be

     for(int i=0;i<length - 1;i++)
    

    since you can have length - 1 number of comparisons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm comparing the following code in C++ and C# and C# (Mono 2.4) seems
I have problem with comparing the value of array elements. e.g. I wanted to
Because jQuery is a widely used and mature collaborative effort, I can't help but
I welcome any help for the following code optimization problem: I have a collection
There exist tools for comparing code against a custom specified set of coding guidelines/standards
Why comparing to null is so unstable? Just code. IronRuby 0.9.4.0 on .NET 2.0.50727.4927
I need some comparing and replacing code in PHP Our mission is to empower
When compiling the following code: void DoSomething(int Numbers[]) { int SomeArray[] = Numbers; }
I've got this HTML form where I have multiple input elements: text, radio, and
One can define a static array at compile time as follows: const std::size_t size

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.