Have you ever struggled with how to solve the ArrayIndexOutOfBoundsException
. If you did I am here to help you on how to solve it and why you might see the exception, But if you didn't just read the article so that it will help you when you encounter the exception.
An array Index Out Of Bounds Exception is thrown when a program attempts to access an element at an index that is outside the bounds of the array. This typically occurs when a program tries to access an element at an index that is less than 0 or greater than or equal to the length of the array.
How to solve the array index out of bounds
I was trying to write a Java program that reads in a data file and reads the integers into a standard integer array (not an ArrayList), sorts the array, and displays the values from lowest to highest. I also needed to write a sort function that uses bubble sort or selection sort to do the sorting. I keept getting this error: "java.lang.ArrayIndexOutOfBoundsException:
Here is the code that was giving me the exception.
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declaring variables
String name;
// Declaring scanner for name of file
Scanner iscanner = new Scanner(System.in);
// Reading in file name
System.out.println("What is the name of your file?" );
name = iscanner.next();
java.io.File file = new java.io.File(name);
// Declaring scanner for inside the file
Scanner inp;
try {
// Assigning scanner to the file
inp = new Scanner(file);
// Declaring array
int [] nums = new int [(int) name.length()];
// Reading integers into array
for (int i =0; i < name.length(); i++)
{
nums[i] = inp.nextInt();
}
// THIS IS LINE 39*****************
// Calling sorting function
bubblesort(nums);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void bubblesort(int[] nums){
int temp;
for (int i = 0; i < nums.length; i++)
{ // THIS IS LINE 52**************************
for (int j = 0; j < nums.length; j++)
{
if(nums[i] > nums[j+1])
{
temp = nums[j+1];
nums[j+1] = nums[i];
nums[i] = temp;
}
}
}
for(int i = 0; i < nums.length; i++)
{
System.out.println(nums[i]);
}
}
The ArrayIndexOutOfBoundsException
comes when trying to access an index of the array that does not exist. In Java, array indices start at 0 and go up to array.length - 1
. This means if you try to access array.length
, it goes out of bounds since that index is not valid.
In my bubblesort
function, the issue was arising from this line:
if(nums[i] > nums[j + 1])
I was using j
to loop through the array with the condition j < nums.length
. That means j
can take values from 0 to nums.length - 1
. When j
is at nums.length - 1
, j + 1
equals nums.length
, which was out of bounds for the array.
To fix this, I changed the inner loop condition so that it does not allow j
to reach the last index when using j + 1
. Specifically, I changed:
for (int j = 0; j < nums.length; j++)
to:
for (int j = 0; j < nums.length - 1; j++)
This adjustment ensured that when j
is at nums.length - 1
, it won't attempt to access nums[j + 1]
, preventing the out-of-bounds exception.
ArrayIndexOutOfBoundsException
is now correctly functioning.
Author Of article : Mercy Read full article