Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

  1. #1
    ratler89 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

    I'm having a problem with my array, the problem is suppose to allow the user to enter in integers creating an array and allowing the user to indicate when they are done populating the array with a value of -1. I'm getting an error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0" on line 32 which is my Userread method where "a[i] = temp;". Can anyone help me!!!!! Here is my code:



    import java.util.Scanner;
    import java.io.*;
    import java.util.*;

    public class RecursiveArray

    {

    /**
    *@param a[] is the array to be filled by the user
    *@param n is the size of the a[]
    *@return i which is the value of the index
    *Used to read the user input into the array, and using -1 to indicate that the user is finished entering integers
    */
    public static int userread(int[]a, int n ){
    Scanner inputDevice = new Scanner(System.in);
    int temp;
    int i = 0;
    do
    {
    System.out.println("Enter an integer(s) or -1 when you are done entering integers");
    temp = inputDevice.nextInt();
    if (temp != -1)
    { a[i] = temp;
    i++;}
    }while ( temp != -1);
    return i;}
    /*
    *@param a[] is the array to be filled by the user
    *@param n is the size of the a[]
    *Used to print out the array that the user has entered
    */
    public static void print(int[] a, int n)
    {
    int i = 0;
    for(i = 0; i < n; ++i){
    System.out.println(a[i] + "");}}

    /*
    *@param arg[] is the args from main()
    *@param n is the size of the a[]
    *@param index used for a counter
    *@param reverseArray is used to hold the indexs but in a reverse way from a[] index
    *Used to reverse the order of the array by using recursive functions
    */
    public static int[] backwards(int[] arg, int[] reverseArray, int n, int index){
    if (n > 0)
    {
    int a = arg[n-1];
    reverseArray[index] = a;
    backwards(arg,reverseArray,n-1,++index);
    }
    return reverseArray;
    }



    public static void main(String[] args)
    {
    int[]a= {};

    int n = a.length;
    int index = 0;
    userread(a, n);
    print(a, n);
    int[] reverseArray = new int[a.length];
    a = backwards(a, reverseArray, n, index);
    for(int i = 0; i < a.length; i++){
    System.out.print(a[i] + " ");
    }

    }


    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

    Your array has size 0 -> int[] a = {};

    for these kind of tasks it would be better to use lists (arraylist, linkedlist, etc.) instead of arrays

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

    Please go through BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ratler89 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

    Ok, so I used a Arraylist but now the program won't lead into my backwards method where i'm trying to do recursion.? and a new error saying: ----jGRASP exec: javac -g RecursiveArray2.java

    RecursiveArray2.java:53: error: missing return statement
    }
    ^
    RecursiveArray2.java:68: error: variable al might not have been initialized
    int n = al.length;
    ^
    Note: RecursiveArray2.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    2 errors




    import java.util.Scanner;
    import java.io.*;
    import java.util.*;

    public class RecursiveArray2

    {

    public static int userread(ArrayList ia ){
    Scanner inputDevice = new Scanner(System.in);
    int temp;
    int i = 0;
    do
    {
    System.out.println("Enter an integer(s) or -1 when you are done entering integers");
    temp = inputDevice.nextInt();
    if (temp != -1)
    { ia.add(i,temp);
    i++;}
    }while ( temp != -1);
    return i;}




    public static void print(ArrayList ia)
    {
    System.out.println("Contents of Your Array is: " + ia); }

    public static Object[] convert(ArrayList ia)
    {
    Object al[]= ia.toArray();
    //int n = al.length;
    return al;}
    //return n;}

    public static int[] backwards(int[] al, int[] reverseArray, int n, int index)
    {
    if (n>0)
    {

    int b = al[n-1];
    int[] reverseBack = new int [b];
    backwards(al, reverseArray, n, ++index);}
    }



    public static void main(String[] args)
    {
    ArrayList ia = new ArrayList();


    int index = 0;
    int[] al;
    int[]reverseBack;
    userread(ia);
    print(ia);
    convert(ia);
    int n = al.length;
    int[] reverseArray = new int[n];
    backwards(al, reverseArray, n, index);
    for (int i=0; i < n; i++)
    System.out.println(al[i] + " ");

    }

    }

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

    Please read Darryl's link.
    Unformatted code really doesn't give us much incentive to read it.
    quad64bit likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 09-19-2012, 09:35 AM
  2. Replies: 4
    Last Post: 01-04-2012, 11:23 PM
  3. Replies: 5
    Last Post: 04-05-2010, 02:28 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •