title: How to remove the last input element in an array?
body: <p>For this program, I have a user input elements into a 5 space array using the <code>private static void add()</code> method. After adding these elements, the user is then able to use the <code>private static void delete()</code> 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 <code>currentSize</code> of the array. For example, if I have an array with the following indexes and values:</p>
  
  <pre><code>0. = 1
  1. = 2
  2. = 3
  3. = 4
  4. = &lt;empty&gt;
  </code></pre>
  
  <p>The <code>currentSize</code> 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 <code>IndexOutOfBoundsException: -1</code> . I believe that this has something to do with <code>currentSize--</code> decrementing when it is not suppose to in the remove element algorithm present in the <code>private static void delete()</code> 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 <code>private static void delete()</code> method that is giving me issues. This is the stack trace that I am getting:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
      at ArrayManager.add(ArrayManager.java:35)
      at ArrayManager.main(ArrayManager.java:216)
  </code></pre>
  
  <hr>
  
  <pre><code>/**
   * 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 &gt;= 1 &amp;&amp; n &lt;= 99)
              {
                  if(currentSize &lt; 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.");
          }
      }
  </code></pre>
  
  <hr>
  
  <pre><code>/**
       * 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 &lt; 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 &lt; 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.");
          }
      }
  </code></pre>
  
  <hr>
  
  <pre><code>/**
       * A method that prints out the modified array.
       */
      public static void printArray()
      {
          System.out.println("* Current Array Contents *");
          for(int i = 0; i &lt; values.length; i++)
          {
              if(values[i] != 0)
              {
                  System.out.println(i + ". = " + values[i]);
              }
              else if(values[i] == 0)
              {
                  System.out.println(i + ". = &lt;empty&gt;");
              }
          }
      }
  </code></pre>