-3

In continuation of this post (Check String is NULL or EMPTY), I managed to successfully write up the if and else logic.

However, my third logic does not work. My third logic is something like checking both empty fields at the same time and if they are empty, the error icon would pop out.

Below are my codes:

    private Boolean checkEmptyField(){
    Boolean result = false;

    String subject = querySubject.getText().toString();
    String message = queryText.getText().toString();

    if(subject.isEmpty()){
        Toast.makeText(this, "Subject/Type field must not be blank!", Toast.LENGTH_SHORT).show();
        querySubjectInfoIcon.setVisibility(View.VISIBLE);
    }else if(message.isEmpty()){
        Toast.makeText(this, "Content field must not be blank!", Toast.LENGTH_SHORT).show();
        newQueryInfoIcon.setVisibility(View.VISIBLE);
    }else if(subject.isEmpty() && message.isEmpty()){
        Toast.makeText(this, "Fields are mandatory & must not be blank!", Toast.LENGTH_SHORT).show();
        newQueryInfoIcon.setVisibility(View.VISIBLE);
        querySubjectInfoIcon.setVisibility(View.VISIBLE);
    }
    else{
        result = true;
    }
    return result;
}

When I run the app and intentionally leave two fields empty, it only checks and display the icon error on one field which is the subject field. This means only one logic (the if subject.isEmpty) is running.

As such I need some advises how I can run the third logic successfully. Thank you.

  • the most constrraint if condition should come earlier in the block. Thats is because the code will execute the first successfully if condition. – akshaya pandey Nov 22 '18 at 06:25

4 Answers4

2

Put the test for both conditions first, once an if matches it will not enter any of the else(s). Like,

if(subject.isEmpty() && message.isEmpty()) {
    Toast.makeText(this, "Fields are mandatory & must not be blank!", Toast.LENGTH_SHORT).show();
    newQueryInfoIcon.setVisibility(View.VISIBLE);
    querySubjectInfoIcon.setVisibility(View.VISIBLE);
} else if(subject.isEmpty()) {
    Toast.makeText(this, "Subject/Type field must not be blank!", Toast.LENGTH_SHORT).show();
    querySubjectInfoIcon.setVisibility(View.VISIBLE);
} else if(message.isEmpty()) {
    Toast.makeText(this, "Content field must not be blank!", Toast.LENGTH_SHORT).show();
    newQueryInfoIcon.setVisibility(View.VISIBLE);
} else {
    result = true;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Once your first if condition triggers, the else if below will not. Check subject.isEmpty() && message.isEmpty() first in the if, then check the other two conditions afterwards.

M.G
  • 440
  • 1
  • 3
  • 10
0
  • You can check it with this:

    if (userName != null && !userName .isEmpty() && !userName .equals("null")) 
    
  • you have another option

    if (TextUtils.isEmpty(null)) {
    
    //  null value
    
    return; // or break, continue, throw
    
    }else{
    
    // string has value
    
    }
    
    Log.i("TAG", myString);
    
Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

Use this

if(subjet == null){
    //blablabla
}

or

if(message == null){
    //blablabla
}
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27