RunnerDude3000
RunnerDude3000

Reputation: 11

How to remove the last input element in an array?

For this program, I have a user input elements into a 5 space array using the private static void add() method. After adding these elements, the user is then able to use the private static void delete() method which allows them to input a number present in the array that they wish to remove. When entering numbers to remove, the program works perfectly fine unless I try to remove the last number for the currentSize of the array. For example, if I have an array with the following indexes and values:

0. = 1
1. = 2
2. = 3
3. = 4
4. = <empty>

The currentSize of the array is currently 4. If I try to remove value 4 in index 3, the program will not remove value 4. If I attempt to remove values 3, 2, or 1 after trying to remove value 4, these values will not remove either. On the other hand if I want to remove any of the values below value 4 first, i.e) values 1, 2, and 3, the program works correctly until I try to remove value 4 in index 0. If I attempt to remove value 4 at this point, nothing is removed. If I try to add a value, say 1, after attempting to remove value 4, value 4 is replaced with value 1. If I attempt to remove value 4 twice at index 0 and then attempt to add a new value, I get an IndexOutOfBoundsException: -1 . I believe that this has something to do with currentSize-- decrementing when it is not suppose to in the remove element algorithm present in the private static void delete() method. If anyone has a solution for this it would be greatly appreciated, thank you. The program is posted below. I have commented the section of the private static void delete() method that is giving me issues. This is the stack trace that I am getting:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at ArrayManager.add(ArrayManager.java:35)
    at ArrayManager.main(ArrayManager.java:216)

/**
 * A class that contains methods to carry out the Add, Update, Delete, Drop, Resize, and End commands to alter the state of an array with 5 integers.
 */
public class ArrayManager
{
    // Array of 5 integers to be modified
    private static int [] values = new int [5];

    private static int currentSize = 0;
    private static int position = 0;
    //private static int index = 0;

    static Scanner in = new Scanner(System.in);

    /**
     * A method that inserts an entered value into the array as long as it is between 1 and 99. If the array is full, an error message will be printed explaining that the array is full.
     */
    private static void add()
    {
        System.out.println("Enter values between 1 and 99, inclusive that you would like to add to the array.");
        if(in.hasNextInt())
        {
            int n = in.nextInt();
            if(n >= 1 && n <= 99)
            {
                if(currentSize < values.length)
                {
                    values[currentSize] = n;
                    currentSize++;
                }
                else
                {
                    System.out.println("ERROR: The array is currently full.");
                }
            }
            else
            {
                System.out.println("ERROR: The number entered must be between 1 and 99, inclusive.");
            }
        }
        else
        {
            System.out.println("ERROR: String has been entered. Enter an Integer.");
        }
    }

/**
     * A method that asks the user to enter a value they wish to delete in the array. The following values are then shifted down in index in the array. If the value chosen does not exist in the array, an error message is displayed explaining that the value entered does not exist in the array.
     */
    private static void delete()
    {
        int n = 0;
        System.out.println("Please enter the value in the array that you wish to remove.");
        if(in.hasNextInt())
        {
            n = in.nextInt();
            for(position = 0; position < values.length; position++)
            {
                if(values[position] == n)
                {
                 // The stack trace points me back to this section of code which removes the specified value in the values array.
                    for(int i = position + 1; i < currentSize; i++)
                    {
                            values[i - 1] = values[i];
                            values[i] = 0;
                    }
                    currentSize--;
                    break;
                }
                else if(position == values.length - 1)
                {
                    System.out.println("ERROR: The value entered does not exist in the array.");
                }
            }
        }
        else
        {
            System.out.println("ERROR: String has been entered. Enter an Integer.");
        }
    }

/**
     * A method that prints out the modified array.
     */
    public static void printArray()
    {
        System.out.println("* Current Array Contents *");
        for(int i = 0; i < values.length; i++)
        {
            if(values[i] != 0)
            {
                System.out.println(i + ". = " + values[i]);
            }
            else if(values[i] == 0)
            {
                System.out.println(i + ". = <empty>");
            }
        }
    }

Upvotes: 1

Views: 4147

Answers (1)

deepak marathe
deepak marathe

Reputation: 408

The corner case where position is the index of last element of the values array, is not handled properly. In such a scenario, the code starts to iterate the elements from the next index in order to shift all the elements by 1 position and the condition is not met by the condition in for loop.

for(int i = position + 1; i < currentSize; i++)

    for(int i = position + 1; i < currentSize; i++)
    {
        values[i - 1] = values[i];
        values[i] = 0;
    }

The solution would be to check for that condition and handle it explicitly.

   if(values[position] == n ) {
       if( position != values.length - 1 ) {
           for(int i = position + 1; i < currentSize; i++)
           {
               values[i - 1] = values[i];
               values[i] = 0;
           }
       } else {
           values[i] = 0;
       }
       currentSize--;
       break;
   } 

Upvotes: 1

Related Questions