check string is null or empty - logic
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
add a comment |
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
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
add a comment |
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
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
java android string logic is-empty
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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;
}
add a comment |
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.
add a comment |
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);
add a comment |
Use this
if(subjet == null){
//blablabla
}
or
if(message == null){
//blablabla
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 22 '18 at 3:53
Elliott FrischElliott Frisch
153k1389178
153k1389178
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 22 '18 at 3:52
M.GM.G
388310
388310
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 22 '18 at 4:41
Android GeekAndroid Geek
4,3991823
4,3991823
add a comment |
add a comment |
Use this
if(subjet == null){
//blablabla
}
or
if(message == null){
//blablabla
}
add a comment |
Use this
if(subjet == null){
//blablabla
}
or
if(message == null){
//blablabla
}
add a comment |
Use this
if(subjet == null){
//blablabla
}
or
if(message == null){
//blablabla
}
Use this
if(subjet == null){
//blablabla
}
or
if(message == null){
//blablabla
}
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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