Home > Net >  Index n out of bounds for length n Exception. I am trying to try to remove duplicates from an array
Index n out of bounds for length n Exception. I am trying to try to remove duplicates from an array

Time:02-18

Expected output: Usage: java Main 1 2 3 4 5 5 to remove duplicates from numbers 1 2 3 4 5 5 java Main 1 2 3 4 5 5 Here are the distinct numbers 1 2 3 4 5

output: Usage: java Main 1 2 3 4 5 5 to remove duplicates from numbers 1 2 3 4 5 5 java Main 1 2 3 4 5 5 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at Main.main(Main.java:26)

/*
Johnny Santamaria
CS 111B Assignment 5 - deDup
This program gives you instructions to remove duplicate numbers in an array
*/
class Main {
  public static void main(String[] args) {
    int[] numArray;
    int index = 0;
    int num;
    
    
    if (args == null || args.length < 1) {
      System.out.println("Usage: java Main 1 2 3 4 5 5 to remove duplicates from the numbers 1 2 3 4 5 5");
      return; //exit function
      }

    //finds integers in command line
    index = args.length;
    numArray = new int[index];
    

    //convert to integers to actually make the array
    for (String arg : args) {
      num = Integer.parseInt(arg);
      numArray[index] = num;
      index  ; 
      }
    
    int uniqueIndex = deDup(numArray, index);   
    }
    
    public static int deDup(int[] numArray, int index) {
      if (index == 0 || index == 1) {
            return index;
      }
      
      
      //creates new index to store unique numbers 
      int uniqueIndex = 0;

      //checks each number in the array to remove duplicates to make a new index
      for (int i = 0; i < index - 1; i  ) {
      //deletes a number in the index of the array then reformats them 
        if (numArray[i] != numArray[i   1]) {
          numArray[uniqueIndex  ] = numArray[i];
        }
      }
      
      numArray[uniqueIndex  ] = numArray[index - 1];

      System.out.println("Here are the distinct numbers: "   numArray[uniqueIndex]);
      
      return uniqueIndex;
    }
  }

CodePudding user response:

The exception message indicates that you are trying to access an index that does not exist:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at Main.main(Main.java:26)

Illegal access occurs on this line:

numArray[index] = num;

The reason is that in numArray there is no index with the value of the index variable.

PS: note that on line 19 you assign the length of args to the variable index.

CodePudding user response:

The problem is that you define these values:

index = args.length;
numArray = new int[index];

and then in the first loop iteration you access

numArray[index] = num;

which is not a valid index since it is too big by 1.

The real problem here is that you are using index as a running variable in your loop, but it is not set to the starting value of 0. You are using the same variable for two different purposes.

Either

  1. set index = 0; before the loop or
  2. use a different variable in your loop, such as i:

int i = 0;
for (String arg : args) {
  num = Integer.parseInt(arg);
  numArray[i  ] = num;
}

CodePudding user response:

add numbers to a set collection is a easy way

  • Related