-2

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;
    }
  }
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • As far as I can tell, this question has nothing to do with either recursion or immutability. Please use tags that actually relate to the question. (Alternatively if there is a connection between the **real** question that you want answered and the tags, make sure that you are actually asking the real question. We can't read your mind ...) – Stephen C Feb 18 '22 at 02:16
  • Ah sorry dude, my current lesson was about immutability and recursion so I might as well put it there. I will keep this in mind when asking questions in the future. – Johnny Santamaria Feb 18 '22 at 03:14
  • @JohnnySantamaria as a side note, the correct way to exit your application is to invoke `System.exit()`. Here is some good read on this topic: https://stackoverflow.com/a/3716015/2851311 – hfontanez Feb 18 '22 at 04:44

2 Answers2

1

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.

0

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;
}
f1sh
  • 11,489
  • 3
  • 25
  • 51
  • 1
    Oh thank you that solved my problem. I now see that I am trying to start my array at index 1 which is supposed to be zero. While my program outputs the duplicates only though I can work from there, thank you so much for catching my small mistake. – Johnny Santamaria Feb 18 '22 at 02:13
  • You are welcome. It is very easy to get stuck with these details. I strongly recommend to set a breakpoint and debug your application, it helps you to understand what happens line by line. – f1sh Feb 18 '22 at 02:15
  • In case you are using IntelliJ IDEA, [here](https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html#stepping) is a tutorial on debugging. It looks very similar in different IDEs. – f1sh Feb 18 '22 at 02:17