check string is null or empty - logic












-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.










share|improve this question























  • 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 6:25
















-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.










share|improve this question























  • 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 6:25














-3












-3








-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.










share|improve this question














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.







java android string logic is-empty






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 3:50







user10504780




















  • 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 6:25



















  • 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 6:25

















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 6:25





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 6:25












4 Answers
4






active

oldest

votes


















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;
}





share|improve this answer































    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.






    share|improve this answer































      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);







      share|improve this answer































        0














        Use this



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


        or



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





        share|improve this answer

























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423619%2fcheck-string-is-null-or-empty-logic%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown
























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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;
          }





          share|improve this answer




























            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;
            }





            share|improve this answer


























              2












              2








              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;
              }





              share|improve this answer













              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;
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 '18 at 3:53









              Elliott FrischElliott Frisch

              153k1389178




              153k1389178

























                  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.






                  share|improve this answer




























                    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.






                    share|improve this answer


























                      0












                      0








                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 22 '18 at 3:52









                      M.GM.G

                      388310




                      388310























                          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);







                          share|improve this answer




























                            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);







                            share|improve this answer


























                              0












                              0








                              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);







                              share|improve this answer















                              • 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);








                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 '18 at 4:41









                              Android GeekAndroid Geek

                              4,3991823




                              4,3991823























                                  0














                                  Use this



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


                                  or



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





                                  share|improve this answer






























                                    0














                                    Use this



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


                                    or



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





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      Use this



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


                                      or



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





                                      share|improve this answer















                                      Use this



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


                                      or



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






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Dec 1 '18 at 6:32









                                      Mohammadreza Khatami

                                      1,083822




                                      1,083822










                                      answered Dec 1 '18 at 3:42









                                      jonry simbolonjonry simbolon

                                      6




                                      6






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423619%2fcheck-string-is-null-or-empty-logic%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Wiesbaden

                                          Marschland

                                          Dieringhausen