3

I have a form and I put the forms data in an intent and then start a new activity and send the data with the intent, but I wanna do a check if the strings are empty I should display and error message. If the strings are not empty the activity can be started.

I've tried the following code but it doesn't seem to be working. If the field is empty it just starts the other activity (I've tried with only 1 field for now, because I don't know how to to it for multiple fields)

    //getting the field values
    String firstname = editTextFirstname.getText().toString();
    String lastname = editTextLastname.getText().toString();
    String amount = editTextBedrag.getText().toString();
    String timespan = spinnerPeriode.getSelectedItem().toString();
    String iban = editTextIBAN.getText().toString();

    if(firstname != null) {
        //putting data in the intent
        intent.putExtra(FIRSTNAME, firstname);
        intent.putExtra(LASTNAME, lastname);
        intent.putExtra(AMOUNT, amount);
        intent.putExtra(TIMESPAN, timespan);
        intent.putExtra(IBAN, iban);

        startActivity(intent);
    }else{
        Toast.makeText(MainActivity.this, "Oops, you forgot to fill in some fields!", Toast.LENGTH_SHORT).show();
    }

6 Answers 6

7

A cleaner way to do this is

public static boolean isAnyStringNullOrEmpty(String... strings) {
    for (String s : strings)
        if (s == null || s.isEmpty())
            return true;
    return false;
}

Then you can call it like this

if (isAnyStringNullOrEmpty(firstname, lastname, amount, timespan, iban)) {
    Toast.makeText(MainActivity.this, "Oops, you forgot to fill in some fields!", Toast.LENGTH_SHORT).show();
} else {

}
Sign up to request clarification or add additional context in comments.

Comments

5

Using apache commons, we can do this as:

boolean valid = StringUtils.isNoneEmpty(firstname, lastname, amount, timespan, iban)

2 Comments

Or I would say StringUtils#isNoneBlank. isNoneEmpty() considers space as a non-empty value where isNoneBlank() will say it's empty.
I appreciate you pointing it out in the comments- However, the question specifically asks for empty. Thankyou for your input though.
2

Change

if(firstname != null)

to

if(!TextUtils.isEmpty(firstname) && !TextUtils.isEmpty(lastname) &&
     !TextUtils.isEmpty(amount) &&!TextUtils.isEmpty(timespan) &&
     !TextUtils.isEmpty(iban))

1 Comment

this answer is not correct, if you blank a field and run this code result is not correct
1

Java 8 + Apache Commons Lang only:

String firstname = editTextFirstname.getText().toString();
String lastname = editTextLastname.getText().toString();
String amount = editTextBedrag.getText().toString();
String timespan = spinnerPeriode.getSelectedItem().toString();
String iban = editTextIBAN.getText().toString();


boolean valid =  Stream.of(firstname, lastname, amount, timespan, iban)
                       .allMatch(StringUtils::isNotBlank);

1 Comment

what if i am gettng multiple strings from server and saving it to string now some of have a value but some of them are empty how to check which are empty and which have some values + how to set the values which are not empty to some specif fields??
0
if(firstname != null && lastname != null && amount != null && timespan != null
 && iban != null & firstname.trim().isEmpty() && !lastname.trim().isEmpty() &&
 !amount .trim().isEmpty() && !timespan.trim().isEmpty() && !iban.trim().isEmpty())

1 Comment

and what if it get any string not equal to null then how to get the value ?
0

Using Java Stream API:

private boolean anyBlank(String...strings) {
   return Stream.of(strings).anyMatch(string -> isBlank(string));
}

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.