0

When I try the following code it gives right answer. But when I try to use a.length it throws ArrayIndexOutOfBoundsException. How to make my code to use a.length?

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int[] a;

    int n = sc.nextInt();
    a = new int[n];

    for(int i = 0; i <= a.length; i++) {
        a[i] = sc.nextInt();
    }

    for(int i = a.length; i >= 0; i--) {
        System.out.println(a[i]);
    }
}
2
  • 3
    there is no element in a at the index a.length, because index range of a will be from 0 to (a.length - 1). Since there is no index, that will throw the Exception. Commented Oct 23, 2015 at 13:33
  • I have answered your question @Nisha please see to it if you are weak in using for loops . Commented Oct 23, 2015 at 13:45

5 Answers 5

5

there is no element in a at the index a.length, because index range of a will be from 0 to (a.length - 1). Since there is no index, that will throw the Exception.

Sign up to request clarification or add additional context in comments.

Comments

0
public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  int[] a;


  int n=sc.nextInt();
  a=new int[n];

  for(int i=0; i<a.length; i++) // Remove = from for loop
    {
    a[i]=sc.nextInt();
  }

  for(int i = a.length - 1; i>0; i--) // Modify here 
  {
    System.out.println(a[i]);
  }

 } 
} 

Or for reverse an array you can use directly as below :

Collections.reverse(Arrays.asList(array));

Comments

0

You can only iterate through a.length-1 since the index position start with 0. so in the above case do the below change and it would work as required

  for(int i=0; i<a.length; i++)
    {
    a[i]=sc.nextInt();
}

Comments

0

You can make use of String functions in JAVA . The StringBuffer class is like a dynamic string with awesome built in methods .

public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();

  String num = String.valueOf(n);
  StringBuffer b = new StringBuffer(num);
  StringBuffer revNum = b.reverse();
  String rev = revNum.toString();
  int reversedNumber = Integer.parseInt(rev);

  System.out.println(reversedNumber);

} 

1 Comment

I tried to execute the code and when I did it print just that number.Will I need to add for loop to enter 3 or more numbers.
0
    for(int i=0; i<=a.length; i++)  // let say a.length=5; 
        {                          // by default all the element start from 
        a[i]=sc.nextInt();        //index zero in array 0 1 2 3 4
    }                            //so there is nothing like a[5]
                                // that's why you are getting arrayOutOfBound 
    for(int i=a.length; i>=0; i--)
    {
        System.out.println(a[i]);
    }

}

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.