Please! need help in removing (or set the element value to 0) one element of an array based on the string value that is passed to the remove method. The array contains string names read from a file.
I don't know if I should post the entire code on the site as I came across this assignment posted on this site. And he was told that he should work on it first.I have seen these examples
if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work return oldValue; } _____________________________________________________________________________ public void delete(int n) { Game[] temp = new Game[games.length - 1]; if (n > 0) { System.arraycopy(games, 0, temp, 0, n); } System.arraycopy(games, n + 1, temp, n, games.length - n - 1); games = temp; }
But they don’t work with my code.
Here’s what I am doing.
In the main I am trying to added names into the array. Which I can do.
Then Remove one of the names in the array.
I am not getting any compilation error. It just displays all the contents of the array.
It will be of Great! help if anyone can solve this problem.
ThankYOU!
public int findLoc(String Name){
int count=0;
int foundLoc = 0;
String CurrentName;
while(count < currentNumber){
CurrentName = StudentArray[count].getName();
count++;
if(CurrentName == Name){
foundLoc = count;
}
}
return foundLoc;
}
public void Remove(String name){
String CurrentName="";
int count=0;
int currentindex = 0;
if(name.equals(null)){
System.out.println("Student not Found");
}
else{
while(count < currentNumber){
CurrentName = StudentArray[count].getName();
count++;
currentindex = findLoc(CurrentName);
System.out.println(+currentNumber);
}
if (currentNumber > 0 && currentindex == currentNumber){
System.arraycopy(StudentArray,currentindex + 1, StudentArray,currentindex, currentNumber - currentindex);
System.out.println("Removed " +name);
}
}
}