0

This seems simple enough but I get the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at reverse.main(reverse.java:28)"

I initially take inputs from the user to write an array, and then I want to print the array backwards. I understand there are other ways of doing this, but I mainly want to know why this is not working. Going through it line by line makes sense?

PS. If it's not a problem, is there any better way of doing this?

import java.util.Scanner;


public class reverse {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.printf("Enter the number of values in array: ");
        Scanner scanner = new Scanner(System.in);
        int n;
        n = scanner.nextInt();

        double[] a1 = new double[n];
        int i;

        System.out.printf("Enter the value in the array: ");
        for (i = 0; i < n; i++){
            Scanner scanner2 = new Scanner(System.in);
            a1[i] = scanner2.nextInt();
            }
         double j;
         double k;

            for (i = 0; i < n/2; i++){
                j = a1[i];
                k = a1[n-i]; //error line;
                a1[i]=k;
                a1[n-i]=j;
            }
         for(i = 0; i < n; i++){
        System.out.println(" "+a1[i]);
    }}


}
3
  • Why create a new Scanner when the old one is already reading from System.in just fine? Commented Feb 20, 2014 at 13:34
  • 1
    You should really learn to format your code, it will do you a favor in a near future, when your programs become more complicated! Commented Feb 20, 2014 at 13:34
  • k = a1[n-i]; When you have minuses and other things when you try to calculate an index, please check before that the index is not out of bounds. Commented Feb 20, 2014 at 13:34

3 Answers 3

6

When i = 0, n-i will result in n, which is one larger than the available indexes( 0 -> n-1 ).

for (i = 0; i < n/2; i++){
  j = a1[i];
  k = a1[n-i]; //error line;
  a1[i]=k;
  a1[n-i]=j;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, that's the explanation, but the code you gave is exactly the same.
I think they gave the same code and an explanation so the guy can fix it himself. Bit better to learn that way, no?
@user898465 For people coming and reading this question later, this will confuse them. Either fix the code or don't include it in the answer (since it implies it's the correct solution).
Or just use my answer below ;)
0
Collections.reverse(Arrays.asList(array))

Will reverse an array for you, then just print its values out. It's great to do these kinds of problems as exercises but if you're ever woring in the industry it's usually better to rely on the Java API for trivial things like this. Probably going to be faster and a lot more simpler than anything you can come up with.

1 Comment

Worth noticing that the array must be declared using a wrapper class (i.e here Double[] array), otherwise it doesn't work.
0

As said by Samhain, when i = 0, then n-i == n, which is greater than the last index of the array (since arrays start with index 0).

The simplest solution is to just subtract an additional 1 from n-i.

j = a1[i];
k = a1[n-i-1];
a1[i]=k;
a1[n-i-1]=j;

Also, creating a new Scanner is totally unnecessary. Just continue to use the first one you created.

for (i = 0; i < n; i++){
    a1[i] = scanner.nextInt();
}

Finally, for what it's worth, if you're using nextInt you don't need to declare your array as a double[] (nor do j and k need to be doubles). You can just use ints.

Here's it running on ideone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.