0

In the following code, if the size of the array is larger than 20, I'm trying to remove anything after 20 from the array. In my loop, I have userinput.remove(20 + i); However, I'm getting that it can't find the symbol remove? I'm not sure why it's doing this if the error.add itself is actually working.

userinput is defined earlier in the code

public static void checknames(String[] userinput){

ArrayList<String> error = new ArrayList<String> ();


    if(userinput.length > 20){

        for(int i=0; i<userinput.length - 20; i++){
            error.add(userinput[20 + i]);
            userinput.remove(20 + i);}
            JOptionPane.showMessageDialog(null, "You can only enter up to 20
            employees. \n The following employees exceed this limit." + error);

            }
        }

5 Answers 5

3

The error is correct - there is no such remove method for arrays. You should either:

  • Use a List instead, like the ArrayList you have used for error.
  • Create a new array which is 1 element shorter, and copy over everything except the element you are trying to remove.
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot call remove an array. You can't change the size of the array. But you could set that element to null:

userinput[20 + i] = null;

Comments

0
userinput.remove(20 + i);

userinput is array of String[]. There is not method remove(..) available for array.

May be you need to set value to null for indexes greater than 20 (or) create a new String array with only first 20 elements and discard userinput.

Comments

0

Try this:

public static void checknames(String[] userinput) {

    List<String> error = new ArrayList<String>();

        for(int i=20; i<userinput.length; i++) {
            error.add(userinput[i]);
            userinput[i] = null;
        }
        JOptionPane.showMessageDialog(null, "You can only enter up to 20
            employees. \n The following employees exceed this limit." + error);

}

Just a few little changes. You should always make ArrayLists like this(with List<...>) on the left-hand side. Also, I got rid of the if statement and slightly changed your loop so you don't need it. And as everyone else mentioned, .remove(...) doesn't work with arrays.

Comments

0

If you insist on keeping the String[], you could delegate the "dirty work" to existing API methods, i.e. Arrays.copyOfRange(Object[] src, int from, int to)


Short, Self Contained, Correct (Compilable), Example:

import java.util.Arrays;

public class R {
    public static String[] trimEmployees(String[] employees, int maxSize) {
        return Arrays.copyOfRange(employees, 0, maxSize);
    }

    public static void main(String[] args) {
        String[] employees = new String[] { "Jennifer", "Paul", "Tori",
                "Zulema", "Donald", "Aleshia", "Melisa", "Angelika", "Elda",
                "Elenor", "Kimber", "Eusebia", "Mike", "Karyn", "Marinda",
                "Titus", "Miki", "Alise", "Liane", "Suzanne", "Dorothy" };
        int max = 20;

        System.out.println(String.format("Input employees (len=%d): %s ",
                employees.length, Arrays.toString(employees)));
        if (employees.length > max) {
            employees = trimEmployees(employees, max);
            System.out.println(String.format("Trimmed employees (len=%d): %s",
                    employees.length, Arrays.toString(employees)));
        }
    }
}

Prints:

Input employees (len=21): [Jennifer, Paul, Tori, Zulema, Donald, Aleshia, Melisa, Angelika, Elda, Elenor, Kimber, Eusebia, Mike, Karyn, Marinda, Titus, Miki, Alise, Liane, Suzanne, Dorothy] 
Trimmed employees (len=20): [Jennifer, Paul, Tori, Zulema, Donald, Aleshia, Melisa, Angelika, Elda, Elenor, Kimber, Eusebia, Mike, Karyn, Marinda, Titus, Miki, Alise, Liane, Suzanne]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.