0

This is my code and Iam getting ArrayIndexoutofBoundsException and cant able to find where

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        int sum=0,sumtotal;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter x");
        int x = sc.nextInt();
        sumtotal = (x+1)*(x+2)/2;
        System.out.println("Enter the size of array");
        int n = sc.nextInt();
        int a[] = new int[n];
        System.out.println("Enter values of array");
        for(int i = 0;i<n;i++){
            a[i] = sc.nextInt();
        }
        for(int i = 0;i<=a.length;i++)
        sum = sum+a[i];
        int miss = sumtotal-sum;
        System.out.println(miss);
    }
}

I put size of array 5 values are 1,2,4,5,6 and value of x is 5

  • 1
    `i<=a.length` must be `i < a.length` – user7 Jan 27 '21 at 13:27
  • 2
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – tibetiroka Jan 27 '21 at 13:40

2 Answers2

0

You need to change the for loop from

 for(int i = 0;i<=a.length;i++)

to

 for(int i = 0;i<a.length;i++)

because the range of i is from 0 to 5 and your array a have index from 0 to 4 only.

Joby Wilson Mathews
  • 9,520
  • 4
  • 50
  • 47
  • [Should you answer questions where the cause of the problem is a typo?](https://meta.stackoverflow.com/questions/366135/should-you-answer-questions-where-the-cause-of-the-problem-is-a-typo) – Lino Jan 27 '21 at 13:35
-1
for(int i = 0;i<=a.length;i++)
        sum = sum+a[i];

If you pay attention to the condition in the loop, you can see how i is less or equal to the array's length. Elements of arrays are only indexed between 0 and length-1, so when you try to access the i. element of the array, it throws an exception, because there is no such element. To fix it, change <=a.length to <a.length.

tibetiroka
  • 1,065
  • 2
  • 11
  • 17
  • 2
    [Should you answer questions where the cause of the problem is a typo?](https://meta.stackoverflow.com/questions/366135/should-you-answer-questions-where-the-cause-of-the-problem-is-a-typo) – Lino Jan 27 '21 at 13:35
  • @Lino I think it is not clear which posts are based on typoes. OP might think that this is how he is supposed to iterate through the array. If you have a look at [this question](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it), (which this is a duplicate of, I'm about to flag it as so), it is not closed. Why? Probably because they considered it as a wrong interpretation of how arrays work. It is important to separate misunderstandings and unintentional mistakes. – tibetiroka Jan 27 '21 at 13:39
  • Then why answer a duplicate question? What does your answer add that the linked duplicate doesn't? – Lino Jan 27 '21 at 13:43
  • We are not supposed to answer duplicate questions either, but according to [this post](https://meta.stackoverflow.com/questions/352194/what-to-do-with-answer-on-duplicate-question) already existing answers don't have to be deleted, but they also shouldn't get upvoted or downvoted if they don't add any value. This is convenient in case the post gets deleted from the site, which is likely if it is such a common duplicate. – tibetiroka Jan 27 '21 at 13:43
  • Just to clarify: I understand that it was not a good action to create this answer. My brain worked a bit slow and didn't realise I was supposed to mark it instead of answering it. You are completely right about that. However, if it is already here, deleting it wouldn't help anything. – tibetiroka Jan 27 '21 at 13:44
  • I can only cite the last paragraph of the answer you linked to: *I feel that answerers should try and find a dupe target before answering as we don't want to blindly duplicate content.*. At last downvotes are still very opinion based and I will downvote all questions and answers where I think they add no value to this site. So don't take this personal. I'm still an internet stranger, that just gave you less internet points – Lino Jan 27 '21 at 13:49
  • You are free to downvote any question or answer that you don't feel helpful. You don't even have to say that you downvoted it; and I can't just assume that you downvoted it. There is just one more thing I'd like to point out: if you read the first comment on the answer in the post you linked, you will find that the question has to be resolved before it can be closed, if it is caused by a typo. This is not so relevant to the current post, but might be useful for the future. – tibetiroka Jan 27 '21 at 13:51