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

I am trying to remove an element from an array by employee number. However, it d

ID: 3627373 • Letter: I

Question

I am trying to remove an element from an array by employee number. However, it does remove the one I wish it to, yet it copies and creates a double copy of another element in the array. Can anyone help me with this?


public void removeEmp()
{
Scanner input = new Scanner (System.in);
int e1 = 0;
int element = -1;//set to negative one to help call if statement
System.out.print(" Enter in the employee number you wish to remove: ");
e1 = input.nextInt();

if(currentEmployees==0)
{
System.out.print(" No Employees added ");
}//endif
if(currentEmployees == 1)
{
currentEmployees = 0;
}
if(currentEmployees > 1)
{
for (int x = 0; x<currentEmployees; x++)
{
if(employees[x].getEmployeeNumber() == e1)//finds which element the employee number is in the array
{
element = x;
}//end if
}//end for
}//end if
if (element != -1)
{
for ( int i = element ; i < currentEmployees - 1 ; i++ )
{
employees[i] = employees[i+1];
}
System.out.print(" Employee Removed! ");
}//end if
else
{
System.out.print(" Employee Number doesn't exist!");
}//end else
}// removeEmp

Explanation / Answer

please rate - thanks

you didn't subtract 1 from the current number of employees

add the line in red, the the last employee is moved up, but display 1 extra employee

public void removeEmp()
{
Scanner input = new Scanner (System.in);
int e1 = 0;
int element = -1;//set to negative one to help call if statement
System.out.print(" Enter in the employee number you wish to remove: ");
e1 = input.nextInt();

if(currentEmployees==0)
{
System.out.print(" No Employees added ");
}//endif
if(currentEmployees == 1)
{
currentEmployees = 0;
}
if(currentEmployees > 1)
{
for (int x = 0; x<currentEmployees; x++)
{
if(employees[x].getEmployeeNumber() == e1)//finds which element the employee number is in the array
{
element = x;
}//end if
}//end for
}//end if
if (element != -1)
{
     for ( int i = element ; i < currentEmployees - 1 ; i++ )
      {
        employees[i] = employees[i+1];
       }

currentEmployees--;
System.out.print(" Employee Removed! ");
}//end if
else
{
System.out.print(" Employee Number doesn't exist!");
}//end else
}// removeEmp

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote