-3

it works properly but when i enter value which is more then 5 it gives exception but not giving me output. If i enter 12 then output should be like this ( 4 , 5 , 1 , 2 , 3)

public static void main(String args[]) {
        int[] a = { 1 , 2 , 3 , 4 , 5 };
        int[] b = new int[5];
        int j = 0,m = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter n:");
        int n = sc.nextInt();
        sc.nextLine();
        m = n;

        for(int i = n ; i < 5 ; i++) {
            b[j] = a[i] - n;
            System.out.println("" +b[j]);
            j++;

        }

        for(int i = 0 ; i < n ; i++) {
            b[j] = a[i] - a[i];
            j++;
        }

        for(int k = 0 ; k < 5 ; k++) {
            System.out.println("a["+k+"]:" +b[k]);
        }
moffeltje
  • 4,521
  • 4
  • 33
  • 57
Dhaval Mistry
  • 476
  • 1
  • 9
  • 17
  • because you used "<5" in print and other for loop. – SMA Dec 30 '16 at 10:44
  • 1
    well, accessing `b[j]` where `j` is `>=5` will result in an `ArrayIndexOutOfBoundException`, because `b` is defined to have the fixed size `5`, though there is no `b[5]`, nor is there any `a[5]`. You might want to initialize both array with the length of the input you did provide – SomeJavaGuy Dec 30 '16 at 10:44
  • so what should i do next? – Dhaval Mistry Dec 30 '16 at 10:46
  • What should be the output when entering a number greater than 5? – moffeltje Dec 30 '16 at 10:51
  • I see why that exception happens (and the link to the other question should tell you too, otherwise ask). I don’t understand what your program was supposed to do or how it was supposed to work, so without some explanation of those I dare not give suggestions for how to remedy. – Ole V.V. Dec 30 '16 at 11:14
  • What should you do next? (1) Read and understand that other question and its answers. (2) If you still don’t know, edit your question to explain why it didn’t help, and what and how your program is supposed to do. – Ole V.V. Dec 30 '16 at 11:16
  • Do you mean `a[i] - a[i]`? This will be 0 always. Indeed if I enter 5, the program prints all 0s. – Ole V.V. Dec 30 '16 at 11:18

1 Answers1

0

As suggested in the comments on your question, the issue is to do with trying to access an element in an array that is not present.

E.g.

int[] array = new int[5];
int boom = array[10]; // Throws the exception

(copied from What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?)

In your example, the problem occurs in the code here:

for(int i = 0 ; i < n ; i++) {
        b[j] = a[i] - a[i];
        j++;
    }

The reason is that you set n to the integer that you pass in (in your example '12') and then loop over i, increasing it each time, while it is less than n.

So using an input of '12', this loop will repeat 12 times with i increasing in value from 0 to 11.

Inside the loop you then try to access the ith element in the a array; which only has 5 elements.

This is fine for the first 5 iterations of the loop (i == 0 ... i == 4), but on the 6th (i == 5) it will try to access an element in the array that is not present; hence the ArrayIndexOutOfBoundException

Hope that makes sense

(Also, note: in the future it's generally a good idea to include a stack trace along with an exception)

Community
  • 1
  • 1
stephendnicholas
  • 1,810
  • 14
  • 15