开发者

Best/MostEfficient way to check if at least one input is entered by user

开发者 https://www.devze.com 2023-03-13 08:56 出处:网络
I am trying to take in multiple parameters from a function and im am checking if at least one of the parameters are not null or empty.

I am trying to take in multiple parameters from a function and im am checking if at least one of the parameters are not null or empty.

right now i am doing something like this.

void foo(String a, String b, String c, String d, ... other strings){

//make sure at least one of the inputs are not null.
if(a!=null || b!=null || c!=null || d!=null ... more strings){
  //do something with the string
}

}

so an input can be foo(null, null, null, "hey"); but it cannot be foo(null, null, null, null);

What my question is is there a better开发者_运维知识库 way to do this, rather than keep adding to the if statement. Im blanking out right now.... Thanks


Use varags

   public static boolean atLeastOneEmpty(String firstString, String... strings){
      if(firstString == null || firstString.isEmpty())
         return true;

      for(String str : strings){
         if(str == null || str.isEmpty())
            return true;
      }
      return false;

    }

Returns true if at least one string is empty


It is a bit late, but from Apache Commons Lange 3.11 you can use the method ObjectUtils.anyNotNull().

0

精彩评论

暂无评论...
验证码 换一张
取 消