Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wrote the following code, I ask the user for input and also elements to be del

ID: 3585351 • Letter: I

Question

I wrote the following code, I ask the user for input and also elements to be deleted (arrays). but for some reason it only deletes the index not the element itself. Any idea how to fix it? Thank you.

public class highArray

{

   private long [] a;

   private int nElems;

  

   public highArray (int max)

   {

       a = new long [max];

       nElems= 0;

   }

  

public void insert (long value)

   {

       a[nElems]= value;

       nElems++;

   }

  

   public boolean detele ( long value)

   {

       int j;

       for (j=0; j<nElems; j++)

           if (value == a[j])

               break;

       if (j==nElems)

           return false;

       else

       {

           for (int k=j; k<nElems; k++)

           a[k-1]= a[k];

           nElems--;

           return true;

           //a[k-1]= a[k];

       }

   }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.util.Scanner;      

public class HighArrayApp {

  

       public static void main( String [] args )

       {

  

           int input;

           char option;

           Scanner scan = new Scanner( System.in );

           System.out.println("Please enter the number of elements:");

           int length=scan.nextInt();

             

          

           highArray arr = new highArray(length);

         

       {

           String menu = " A) Insert a Data:";

           menu += " B) Remove a Data:";

           menu += " C) Search a Data:";

           menu += " D) Display all Data";

           menu += " E) Quit ";

          

           System.out.println( ""Please select one of the following options: " );

          

           System.out.println( menu ); // print the menu

           System.out.print( "Make a selection "

           + "or "E" to Quit > " );

          

           option = scan.next( ).charAt( 0 );

           while ( option != 'E' && option != 'e')

           {

           switch ( option )

           {

           case 'A':

      case 'a':

           for( int i =0;i<length;i++)

             

           {

           System.out.print( "Please insert your data "+"["+(i+1)+"]"+":");

           input=scan.nextInt();

           arr.insert(input);

           }

          

      

           System.out.println( menu );

           break;

             

      case 'B':

           case 'b':

           System.out.print( "Please insert data to be removed: " );

           input= scan.nextInt();

           if

           (arr.detele(input))

           System.out.println("Element has been removed from the array list");

           else

           System.out.println("Element can't be removed because it is not found in the array");

          

           System.out.println( menu );

           break;

Explanation / Answer

Please replace the code of your delete method with mine and test it. I hope it works properly. If it doesn't reply in the comments so that I will check again.

**CODE of method delete:

public boolean detele ( long value)
    {
    int flag = 1, loc;
    for (int i = 0; i < nElems; i++)
        {
            if(a[i] == value)
            {
                flag =1;
                loc = i;
                break;
            }
            else
            {
                flag = 0;
            }
        }
   
    if(flag == 1)
        {
            for(int i = loc+1; i < nElems; i++)
            {
                a[i-1] = a[i];
            }
            return true;
        }
        else
        {
            return false;
        }
    }