0

Hello everyone i am trying to remove an name that the user has put in from an String Array, i am new to programming and i have tried this but it doesn't work. Can someone help me or tell me what i am doing wrong?

    String [] myName = {"Testname","Charel","melissa","Kelly"};

   removeName(myName);

    public void removeName(String[] names )
    {
        Scanner sc =  new Scanner(System.in);
        String name = "";
         name = sc.nextLine();
        for (int i = 0; i < names.length; i++) {
           name = names[i-1];
        }
    }

How can i do this?

mentallurg
  • 4,967
  • 5
  • 28
  • 36
Kelly
  • 1
  • What does "remove" mean here, what result are you expecting? The length of an array is fixed, so you cannot make it shorter by removing an element. You are probably better off using a `List` or some other type of collection instead of an array for this. – kaya3 Dec 15 '19 at 22:26
  • so instead of 4 name i get 3 names – Kelly Dec 15 '19 at 22:28
  • 1
    You cannot change an array from length 4 to length 3. Arrays are fixed-length. – kaya3 Dec 15 '19 at 22:28
  • 3
    Use a `List`, or create a **new** array. – Elliott Frisch Dec 15 '19 at 22:29

5 Answers5

0

You probably need to use Lists for this. Your list will be a list of String, and use remove() method to do this.

An array's length is fixed and can't be changed this way.

Useful Link : Removing items from a list

N_Craft
  • 70
  • 7
0

First off, an array does not change size after it is initialized, the only way to change the size of an array is to replace it with a new array! So in order to not end up with a double entry or an empty field, you would need to make a new array that is one size shorter, and write the names you want to keep into that.

An array might be ill-suited for your purposes, so consider using a list or an ArrayList. A list can be resized, so removing an element will automatically shorten the list. I recommend you look into that.

Lastly, you currently aren't even comparing your input to your fields. Replace name = names[i-1]; with something along the lines of

if(name.equals(names[i]))
//TODO: Remove from list

See here for more details about String.equals()!

Also, keep in mind that the user input might not match any name at all, so prepare for that case as well!

LlamaSage
  • 73
  • 6
0

To remove an element from an array in Java, you need to create a new array and copy over all the elements you want to keep. That is because Java arrays are fixed-size.

For example, to remove an element at a particular index, you could do it like this:

public static String[] remove(String[] array, int index) {
    String[] result = new String[array.length - 1];
    System.arraycopy(array, 0, result, 0, index);
    System.arraycopy(array, index + 1, result, index, result.length - index);
    return result;
}

You would then remove melissa from your array as follows:

String[] names = { "Testname", "Charel", "Melissa", "Kelly" };
names = remove(names, 2);
System.out.println(Arrays.toString(names));

Output

[Testname, Charel, Kelly]

Of course, it would be much easier to do it using a List:

List<String> names = new ArrayList<>(Arrays.asList("Testname", "Charel", "Melissa", "Kelly"));
names.remove(2);
System.out.println(names);

Or:

List<String> names = new ArrayList<>(Arrays.asList("Testname", "Charel", "Melissa", "Kelly"));
names.remove("Melissa");
System.out.println(names);

Output of both is the same as above.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

There are some simple methods using java api provide by jdk, for example:

    String [] myName = {"Testname","Charel","melissa","Kelly"};
    List<String> container = new ArrayList(Arrays.asList(myName));
    container.remove("Charel");
    String[] result = new String[myName.length - 1];
    container.toArray(result);

Alternatively you can also use this to convert array to list,

    Collections.addAll(container, myName);
0
String [] myName = {"Testname","Charel","melissa","Kelly"};

removeName(myName);

public void removeName(String[] names )
{
    Scanner sc =  new Scanner(System.in);
    String name = sc.nextLine();

    for (int i = 0; i < names.length; i++) {
        if(names[i]==name)
        {
            for(int j=i;j<names.length-1;j++)
            {
                names[j]=names[j+1];
            }
        }
    }
}
Mark Davies
  • 1,447
  • 1
  • 15
  • 30