0

Im doing an assignment from our Array Operations topic. All operations are working but the delete wont work. Below is the error I got when I tried to delete an element to an array:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at com.company.Array.removeAt(Array.java:34)
at com.company.Main.main(Main.java:67)

Here is my main java:

import java.util.*;

public class Main {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("====================================");
    System.out.print("Enter the Size of the Array: ");
    int arrayLength = input.nextInt();

    Array numbers= new Array(arrayLength);

    int c=1;

    System.out.println("\nInput the numbers consecutively: ");
    for (int i = 0; i < arrayLength; i++) {
        int arrayData = input.nextInt();
        numbers.insert(arrayData);
    }
    System.out.println("\nArray Current Data:");
    numbers.print();
    System.out.println("====================================");

    while(c!=4) {
        System.out.println("Pick an Option: ");
        System.out.println("Select 1 to Add");
        System.out.println("Select 2 to Search");
        System.out.println("Select 3 to Delete");
        System.out.println("Select 4 to Exit");
        System.out.print("= ");
        int option = input.nextInt();

        //Inserting data to array
        if (option == 1) {
            System.out.print("\nEnter Number of Data to Add: ");
            int arrayLength2 = input.nextInt();

            System.out.println("\nInput the numbers consecutively: ");
            for (int i = 0; i < arrayLength2; i++) {
                int arrayData = input.nextInt();
                numbers.insert(arrayData);
            }
            System.out.println("\nArray Current Data:");
            numbers.print();
            System.out.println("====================================");
        }

        //Searching a number to Array
        else if (option == 2) {
            System.out.print("\nInput the number to search: ");
            int arraySearch = input.nextInt();

            System.out.println("\nArray Current Data:");
            numbers.print();

            System.out.println("\nIndex Number of "+ arraySearch + " is " + numbers.search(arraySearch));
            System.out.println("====================================");
        }

        //Deleting a number to Array
        else if (option == 3) {
            System.out.print("\nInput the index number to delete: ");
            int arrayDelete = input.nextInt();

            numbers.removeAt(arrayDelete);

            System.out.println("\nArray Current Data:");
            numbers.print();
            System.out.println("====================================");
        }

        else {
            System.out.println("\nThank you for using!");
            break;
        }
    }
}
 }

Here is my Array java:

public class Array {
private int[] items;
private int count;

public Array(int length) {
    items = new int[length];
}

public void print() {
    for (int i = 0; i < count; i++)
        System.out.println(items[i]);
}

public void insert(int item){
    if(items.length==count) {
        int[] newItems = new int[count * 2];

        for(int i=0; i<count; i++)
            newItems[i] = items[i];
        items=newItems;
    }

    items[count]=item;
    count++;
}

public void removeAt(int index) {
    if(index < 0 || index >= count){
        throw new IllegalArgumentException();}

    for(int i = index; i < count; i++)
        items[i] = items[i + 1];
        count--;
}

public int search(int item){
    for(int i=0; i<count ; i++)
        if(items[i] == item)
            return i;

    return -1;
}

}

The error is pertaining to line 34 in my Array java and line 67 in my Main Java. I don't notice anything wrong. Help me to solve this! I'm also a beginner from java.

Inspirit C
  • 31
  • 9
  • 1
    Change `if(index < 0 || index >= count)` to `if(index < 0 || index > count - 1)` in `removeAt(int index)`, your bounds check is not accounting for arrays being 0-indexed. – Jonny Henly Oct 15 '20 at 01:15
  • 1
    `items[i] = items[i + 1];` - what do you think happens when `i` is at its max value? – Scary Wombat Oct 15 '20 at 01:16
  • 1
    The indentation of `count--` in `removeAt(int index)` is pretty misleading, you should probably de-indent that line, also using curly braces can end up saving a lot of time when it comes to debugging. – Jonny Henly Oct 15 '20 at 01:19
  • I've already did what you said Mr. Henly but it still gives the same error. This is now the code: public void removeAt(int index) { if(index < 0 || index > count - 1){ throw new IllegalArgumentException();} for(int i = index; i < count; i++) items[i] = items[i + 1]; count--; } – Inspirit C Oct 15 '20 at 01:51
  • When i reaches its max value? Im sorry but I dont know :(( – Inspirit C Oct 15 '20 at 01:53

0 Answers0