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] + " ");
}
}
}
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
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
Please go through BB Code List - Java Programming Forum and edit your post accordingly.
db
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] + " ");
}
}
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.