Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is what I currently have:

public Spherocylinder deleteSpherocylinder(String labelIn)
{
   Spherocylinder result = null;
   int index = 0;

   for (int i = 0; i < elements; i++)
   {
      if (object[i].getLabel().equalsIgnoreCase(labelIn))
      {
         for (int j = i; j < elements - 1; j++)
         {
            object[j] = object[j + 1];
         }
         elements--;
         result = object[elements];
         break;
      }


   }

   return result;
}


It does what it is supposed to do which is delete(overwrite) a spherocylinder from my array but when I submit my code for a grade this error comes up:


*Method deleteSpherocylinder of class SpherocylinderList: Method did not return the correct deleted spherocylinder.

*Method deleteSpherocylinder of class SpherocylinderList: Method did not return the correct deleted spherocylinder when using different type casing (ex. SmAlL ExAmPle).

I ran my code and It does all of this and runs perfectly with no problem, could anyone please tell me if anything is wrong with my method, any help would be greatly appreciated.

What I have tried:

i tried:

public Spherocylinder deleteSpherocylinder(String labelIn)
{
   Spherocylinder result = null;
   int index = 0;

   for (int i = 0; i < elements; i++)
   {
      if (object[i].getLabel().equalsIgnoreCase(labelIn))
      {
         for (int j = i; j < elements - 1; j++)
         {
            object[j] = object[j + 1];
            result = object[j];
         }
         object[elements - 1] = null;
         elements--;
         result = object[elements];
         break;
      }


   }

   return result;
}


but it returns null instead of the deleted(overwritten) array. So far this code below seems do exactly as I want but the grading software doesn't like it and thats where im confused.


public Spherocylinder deleteSpherocylinder(String labelIn)
{
   Spherocylinder result = null;
   int index = 0;

   for (int i = 0; i < elements; i++)
   {
      if (object[i].getLabel().equalsIgnoreCase(labelIn))
      {
         for (int j = i; j < elements - 1; j++)
         {
            object[j] = object[j + 1];
            //result = object[j];
         }
         //object[elements - 1] = null;
         elements--;
         result = object[elements];
         break;
      }


   }

   return result;
}
Posted
Updated 28-Oct-17 10:20am

1 solution

Use the debugger - put a breakpoint on the line:
for (int j = i; j < elements - 1; j++)

And find out from that exactly which element you are deleting. Then follow the code and find out which element you are returning.

It's pretty obvious what's wrong, and why - and the debugger will show you if you look carefully.
 
Share this answer
 
Comments
Member 13491066 30-Oct-17 19:48pm    
Hey I cant seem to figure it out could you be more specific.
OriginalGriff 31-Oct-17 2:20am    
What did the debugger show you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900