Search code examples
javaandroidstring-comparison

Compare multiple strings android


what I am trying to do is check if multiple srings are null in order to go to the next activity, something like this:

if( string1.equal(null) && ... stringN.equal(null))
{ Toast.makeText(this, "something is empty", Toast.LENGTH_SHORT).show();}
else
{ GO TO THE NEXT ACTIVITY}

all of the strings have to have content... Any help would be great


Solution

  • If you have have a variable number of strings to check I would do something like the following:

    private boolean validate(String... items) {
      for (String item : items) {
        if (TextUtils.isEmpty(item)) {
          return false;
        }
      }
      return true;
    }
    

    Then your code would call the validate method with as many strings as you need to validate:

    if (validate(string1, string2, stringN)) {
      Toast.makeText(this, "something is empty", Toast.LENGTH_SHORT).show();
    } else { 
      // GO TO THE NEXT ACTIVITY
    }