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);
	 	}
 			
		}
	}

Do you want to "set the element to 0" or do you want to remove it. The sample code will actually remove it. If all you want to do is set it to 0, then there is no reason to "record" the index when you find it, simply set it to zero then and there (or, more probably null, or setName to null or "", since the array contains objects, seemingly).

Thanks for the fast response. masijade
I tried that and its not working! .I think i am missing something.
I have changed my code and edited it to work this way.
I want to remove the currentindex element in the array.
I am still trying to work on your suggestion.

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Student {

	private String name;
	private int age;
	private String major;
	
	public Student(String name,int age,String major) {
		this.name = name;
		this.age = age;
		this.major = major;
	}
	public String getName(){
		return name;
	}
	}

class Group extends Student{
	
	private int capacity;
	private int currentNumber;
	private  Student[] StudentArray = new Student[20] ;
	
	public Group(int capacity){
		this.capacity = capacity;
		
	}
	public boolean Add(Student S){
		if (currentNumber == capacity){
			System.out.println("capacity reached,student not added");
			return false;
		}
		else {
			StudentArray[currentNumber] = S;
			currentNumber = currentNumber+1;
			System.out.println("Added   " +S.getName());
			return true;
		}
	}
	
	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);
	 	}
 			
		}
	}
	 
	
	
public static class main{
	public static void main(String[] args) {
		
		Group G = new Group(4);
		Student studObject = new Student("Danny",123,"MS") ;
		Student studObject1 = new Student("Mark",123,"MS") ;
		Student studObject2 = new Student("Will",12,"MBA") ;
		
		G.Add(studObject);
		G.Add(studObject1);
		G.Add(studObject2);
		G.Remove("Danny");

	}

}
}

Why is there so much code in your remove method? All you need to do is search linearly through your array looking for an item with the name that matches the String you're searching for. That should take maybe three lines or so of code.

method remove(string removeMe)
for (every element in the array)
if (current element isEqualTo removeMe)
then remove the current element from the array

Hi MasiJade

Thank you for your suggestion. I tried to set the element to null if the names match. but when I am trying to print the array elements it will give me a null pointer exception.

Thank You BestJewSinceJC

I tried that already and it works fine for that part. My next part is to print the array elements. which will fail with null value.

Thanks masijade and BestJewSinceJC.
I followed your advice and resolved the issue. I just had to handle the nulls in the array after that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.