Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I get a java.lang.ArrayIndexOutOfBoundsException error when I run a java program

ID: 3758784 • Letter: I

Question

I get a java.lang.ArrayIndexOutOfBoundsException error when I run a java program I am trying to write. I have tried several different approaches and still cannot seem to get it to run past the bolded line of code in the following excerpt from my project. Can anyone just give me a little direction into what I am not writing correctly in this code to keep getting this error?

public static void main(String[] args) {
// Bubble sort program
        //Get user input for number
        Scanner input = new Scanner (System.in);
        System.out.println("Enter a number please: ");
        int n = input.nextInt();
      
        //Get user input for number if iterations
        System.out.println("Enter a number of iterations please: ");
        int numIfor = input.nextInt();
                      
        //create array of random numbers
        int[] randomArray = new int[n];
        Random bubbleRandom = new Random();
      
        //fill in the array of random numbers
        for(int i=0; i < n; i++) {
            randomArray[i] = bubbleRandom.nextInt(100);
        }
        //Printing the array before the sort
        System.out.println("The numbers before the Bubble Sort: ");
        for(int i=0; i < n; i++) {
            System.out.print(randomArray[i] + " ");
        }
        System.out.println();
        //Printing the array out after the Bubble Sort
        int bubble = 0;
        int sort = 0;
        long startTime = System.currentTimeMillis();      
      
            for (int c=0; c < (numIfor - 1); c++)
        {
            for (int d=0; d < numIfor-c-1; d++)
        {
            if (randomArray[d]>randomArray[d+1])
        {
            bubble = randomArray[d];
            randomArray[d] = randomArray[d+1];
            randomArray[d+1] = bubble;
            sort++;

Explanation / Answer

it happens because you are triying to read a value in the array that it doesn't exist.

which value???

well the for cycle to d goes from 0 to numIfor-c-1

supose that the array is 1 2 3 4 5

it have 5 values randomArray[0].... randomArray[4]

but if you have NumIfor=100

"d" eventualy will be grreater than 4 and show you that error.

i try to solve this error modifiying the for cycle; i hope it helps you

import java.util.Random;
import java.util.Scanner;
public class bubble {
public static void main(String[] args) {
// Bubble sort program
//Get user input for number
Scanner input = new Scanner (System.in);
System.out.println("Enter a number please: ");
int n = input.nextInt();
  
//Get user input for number if iterations
System.out.println("Enter a number of iterations please: ");
int numIfor = input.nextInt();
  
//create array of random numbers
int[] randomArray = new int[n];
Random bubbleRandom = new Random();
  
//fill in the array of random numbers
for(int i=0; i < n; i++) {
randomArray[i] = bubbleRandom.nextInt(100);
}
//Printing the array before the sort
System.out.println("The numbers before the Bubble Sort: ");
for(int i=0; i < n; i++) {
System.out.print(randomArray[i] + " ");
}
System.out.println();
//Printing the array out after the Bubble Sort
int bubble = 0;
int sort = 0;
long startTime = System.currentTimeMillis();
  
for (int c=0; c < (numIfor - 1); c++)
{
for (int d=0; d < n-1; d++)
{
if (randomArray[d]>randomArray[d+1])
{
bubble = randomArray[d];
randomArray[d] = randomArray[d+1];
randomArray[d+1] = bubble;
//sort++;
}
}
}
System.out.println("The numbers after the Bubble Sort: ");
for(int i=0; i < n; i++) {
System.out.print(randomArray[i] + " ");
}
System.out.println();
}
}

/***************************************************************************

if you have any question about it please leave me a comment

*****************************************************************************/