0

How can I check if an element in a string array is empty? This is my example array:

private static String correct[] = new String[5];
    static {
        correct[1] = "some texT";
        correct[2] = "some texT3";
        correct[4] = "some texT2";
}

I can assign null to rest of elements, but I want to find another, better way to do this. I found isEmpty, but it is available only on API 9 and above.

if(correct[0].length() > 0) gives me a NPE. if(correct[0] != null also.

6 Answers 6

6

Have you tried the 'obvious':

if(correct[0] != null && correct[0].length() > 0) {
   //it is not null and it is not empty
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just compare it to null, which is the default value for an array of some object type.

if (correct[0] != null && correct[0].length() > 0)

The && operator will only evaluate the right side if the left side is true, i.e. correct[0] isn't null and won't throw a NullPointerException.

Comments

1

If you're trying to check if a specific element in a specific position is empty (not only different from null, but from "" too), the presented solutions won't be enough.

Java has a lot of ways to do this. Let's see some of them:

  1. You can convert your array into a List (using Arrays's asList method) and check the conditions through contains() method:

    import java.util.*;
    
    public class Test1 {
    
      private static boolean method1_UsingArrays(String[] array) {
       return Arrays.asList(array).contains(null) 
                || Arrays.asList(array).contains("");
      }
    }
    
  2. It's also possible to use Collections's disjoint method, to check whether two collections have elements in common or not:

    import java.util.*;
    
    public class Test2 {      
    
      private static String[] nullAndEmpty = {"", null};
    
      private static boolean method2_UsingCollectionsDisjoint(String[] array) {
       // disjoint returns true if the two specified collections have no elements in common.
       return !Collections.disjoint( //
         Arrays.asList(array), //
         Arrays.asList(nullAndEmpty) //
       );
      }
    
    }
    
  3. If you're able to use Java 8 (my favorite option), it's possible to make use of the new Streams API.

    import java.util.stream.*;
    import java.util.*;
    
    public class Test3 {
    
      private static boolean method3_UsingJava8Streams(String[] array) {
       return Stream.of(array).anyMatch(x -> x == null || "".equals(x));
      }
    
    }
    

    Compared to the other options, this is even easier to handle in case you need to trim each of your strings (or call any of the other String's methods):

    Stream.of(array).anyMatch(x -> x == null || "".equals(x.trim()));
    

Then, you can easily call and test them:

public static void main(String[] args) {
  // Ok tests
  String[] ok = {"a", "b", "c"};

  System.out.println("Method 1 (Arrays - contains): " + method1_UsingArrays(ok)); // false
  System.out.println("Method 2 (Disjoint): " + method2_UsingCollectionsDisjoint(ok)); // false
  System.out.println("Method 3 (Java 8 Streams): " + method3_UsingJava8Streams(ok)); // false

  System.out.println("-------------------------------");

  // Nok tests
  String[] nok = {"a", null, "c"};

  System.out.println("Method 1 (Arrays - contains): " + method1_UsingArrays(nok)); // true
  System.out.println("Method 2 (Disjoint): " + method2_UsingCollectionsDisjoint(nok)); // true
  System.out.println("Method 3 (Java 8 Streams): " + method3_UsingJava8Streams(nok)); // true

  // Nok tests
  String[] nok2 = {"a", "", "c"};

  System.out.println("Method 1 (Arrays - contains): " + method1_UsingArrays(nok2)); // true
  System.out.println("Method 2 (Disjoint): " + method2_UsingCollectionsDisjoint(nok2)); // true
  System.out.println("Method 3 (Java 8 Streams): " + method3_UsingJava8Streams(nok2)); // true
}

Comments

0

Well if you can't use isEmpty try to use Array.Contains(null) or try this small loop

for(int i = 0;i<=Array.length;i++){
  if(array[i] > 0 && array[i] != null){
     //yay its not null
  }
}

Comments

0

Had similar version where number of arguments could be bigger

if (Arrays.asList(new String[] {productId, productTitle, imageName, productPrice}).contains(null)) {
        return null;
}

Comments

-1

Just Use isAnyEmpty

org.apache.commons.lang3
StringUtils.isAnyEmpty(final CharSequence... css)

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.