Checking for empty string using .equals(), .isEmpty() and .length() in Java
I am writing some code to check if two strings are anagrams, in Java. This is for the Anagrams problem on Hackerrank.
My code is failing only 1 of the 17 test cases, and I'm guessing it's due to invalid inputs. This is where my issue lies. At the very beginning of the method, I want to check if either input string is null or an empty string, and if it is, I return false.
This is the relevant code block -
private static boolean isAnagram(String a, String b) {
System.out.println(a + " " + b);
if( a == null || b == null || a.equals("") || b.equals("") )
System.out.println("Inside");
return false;
HashMap<String, Integer> map1 = new HashMap<>();
HashMap<String, Integer> map2 = new HashMap<>();
The problem is, when I test this method with invalid inputs, like "hello" and "", the if block doesn't seem to execute. I've tried replacing the .equals() method with .isEmpty() and .length() == 0, but no matter which method I use, the if doesn't work.
Using JShell, I've checked that if I create an empty string with "", all these methods work -
jshell> String a = ""
a ==> ""
jshell> a.isEmpty()
$2 ==> true
jshell> a.length()
$3 ==> 0
jshell> a.equals("")
$4 ==> true
jshell> a == null
$5 ==> false
jshell> "" == ""
$6 ==> true
jshell>
jshell> "".equals("")
$7 ==> true
jshell> "".length()
Is there a reason why this way of checking for empty string won't work in an if block?
UPDATE - Complete code on Pastebin
java if-statement
add a comment |
I am writing some code to check if two strings are anagrams, in Java. This is for the Anagrams problem on Hackerrank.
My code is failing only 1 of the 17 test cases, and I'm guessing it's due to invalid inputs. This is where my issue lies. At the very beginning of the method, I want to check if either input string is null or an empty string, and if it is, I return false.
This is the relevant code block -
private static boolean isAnagram(String a, String b) {
System.out.println(a + " " + b);
if( a == null || b == null || a.equals("") || b.equals("") )
System.out.println("Inside");
return false;
HashMap<String, Integer> map1 = new HashMap<>();
HashMap<String, Integer> map2 = new HashMap<>();
The problem is, when I test this method with invalid inputs, like "hello" and "", the if block doesn't seem to execute. I've tried replacing the .equals() method with .isEmpty() and .length() == 0, but no matter which method I use, the if doesn't work.
Using JShell, I've checked that if I create an empty string with "", all these methods work -
jshell> String a = ""
a ==> ""
jshell> a.isEmpty()
$2 ==> true
jshell> a.length()
$3 ==> 0
jshell> a.equals("")
$4 ==> true
jshell> a == null
$5 ==> false
jshell> "" == ""
$6 ==> true
jshell>
jshell> "".equals("")
$7 ==> true
jshell> "".length()
Is there a reason why this way of checking for empty string won't work in an if block?
UPDATE - Complete code on Pastebin
java if-statement
Maybe the invalid input is something like two spaces. Then your if statement is not executed. in this case you have to trim the input first.
– tomas
Nov 12 '18 at 7:32
Your code works for me. Could you show code with test cases?
– sawim
Nov 12 '18 at 7:33
Try with and operator ` if( (a == null && b == null) &&( a.equals("") && b.equals("")) )...`
– SaviNuclear
Nov 12 '18 at 7:34
@sawim I uploaded the complete code using a pastebin link. I don't have access to the failing test case (#8) because it's locked.
– RuchikaSingh
Nov 12 '18 at 7:41
What if a and b are having just whitespaces instead of empty string? I am guessing you should be writing your if statement as a == null || b == null || a.trim().equals("") || b.trim().equals("")
– Mohamed Anees A
Nov 12 '18 at 7:42
add a comment |
I am writing some code to check if two strings are anagrams, in Java. This is for the Anagrams problem on Hackerrank.
My code is failing only 1 of the 17 test cases, and I'm guessing it's due to invalid inputs. This is where my issue lies. At the very beginning of the method, I want to check if either input string is null or an empty string, and if it is, I return false.
This is the relevant code block -
private static boolean isAnagram(String a, String b) {
System.out.println(a + " " + b);
if( a == null || b == null || a.equals("") || b.equals("") )
System.out.println("Inside");
return false;
HashMap<String, Integer> map1 = new HashMap<>();
HashMap<String, Integer> map2 = new HashMap<>();
The problem is, when I test this method with invalid inputs, like "hello" and "", the if block doesn't seem to execute. I've tried replacing the .equals() method with .isEmpty() and .length() == 0, but no matter which method I use, the if doesn't work.
Using JShell, I've checked that if I create an empty string with "", all these methods work -
jshell> String a = ""
a ==> ""
jshell> a.isEmpty()
$2 ==> true
jshell> a.length()
$3 ==> 0
jshell> a.equals("")
$4 ==> true
jshell> a == null
$5 ==> false
jshell> "" == ""
$6 ==> true
jshell>
jshell> "".equals("")
$7 ==> true
jshell> "".length()
Is there a reason why this way of checking for empty string won't work in an if block?
UPDATE - Complete code on Pastebin
java if-statement
I am writing some code to check if two strings are anagrams, in Java. This is for the Anagrams problem on Hackerrank.
My code is failing only 1 of the 17 test cases, and I'm guessing it's due to invalid inputs. This is where my issue lies. At the very beginning of the method, I want to check if either input string is null or an empty string, and if it is, I return false.
This is the relevant code block -
private static boolean isAnagram(String a, String b) {
System.out.println(a + " " + b);
if( a == null || b == null || a.equals("") || b.equals("") )
System.out.println("Inside");
return false;
HashMap<String, Integer> map1 = new HashMap<>();
HashMap<String, Integer> map2 = new HashMap<>();
The problem is, when I test this method with invalid inputs, like "hello" and "", the if block doesn't seem to execute. I've tried replacing the .equals() method with .isEmpty() and .length() == 0, but no matter which method I use, the if doesn't work.
Using JShell, I've checked that if I create an empty string with "", all these methods work -
jshell> String a = ""
a ==> ""
jshell> a.isEmpty()
$2 ==> true
jshell> a.length()
$3 ==> 0
jshell> a.equals("")
$4 ==> true
jshell> a == null
$5 ==> false
jshell> "" == ""
$6 ==> true
jshell>
jshell> "".equals("")
$7 ==> true
jshell> "".length()
Is there a reason why this way of checking for empty string won't work in an if block?
UPDATE - Complete code on Pastebin
java if-statement
java if-statement
edited Nov 12 '18 at 7:40
RuchikaSingh
asked Nov 12 '18 at 7:27
RuchikaSinghRuchikaSingh
62
62
Maybe the invalid input is something like two spaces. Then your if statement is not executed. in this case you have to trim the input first.
– tomas
Nov 12 '18 at 7:32
Your code works for me. Could you show code with test cases?
– sawim
Nov 12 '18 at 7:33
Try with and operator ` if( (a == null && b == null) &&( a.equals("") && b.equals("")) )...`
– SaviNuclear
Nov 12 '18 at 7:34
@sawim I uploaded the complete code using a pastebin link. I don't have access to the failing test case (#8) because it's locked.
– RuchikaSingh
Nov 12 '18 at 7:41
What if a and b are having just whitespaces instead of empty string? I am guessing you should be writing your if statement as a == null || b == null || a.trim().equals("") || b.trim().equals("")
– Mohamed Anees A
Nov 12 '18 at 7:42
add a comment |
Maybe the invalid input is something like two spaces. Then your if statement is not executed. in this case you have to trim the input first.
– tomas
Nov 12 '18 at 7:32
Your code works for me. Could you show code with test cases?
– sawim
Nov 12 '18 at 7:33
Try with and operator ` if( (a == null && b == null) &&( a.equals("") && b.equals("")) )...`
– SaviNuclear
Nov 12 '18 at 7:34
@sawim I uploaded the complete code using a pastebin link. I don't have access to the failing test case (#8) because it's locked.
– RuchikaSingh
Nov 12 '18 at 7:41
What if a and b are having just whitespaces instead of empty string? I am guessing you should be writing your if statement as a == null || b == null || a.trim().equals("") || b.trim().equals("")
– Mohamed Anees A
Nov 12 '18 at 7:42
Maybe the invalid input is something like two spaces. Then your if statement is not executed. in this case you have to trim the input first.
– tomas
Nov 12 '18 at 7:32
Maybe the invalid input is something like two spaces. Then your if statement is not executed. in this case you have to trim the input first.
– tomas
Nov 12 '18 at 7:32
Your code works for me. Could you show code with test cases?
– sawim
Nov 12 '18 at 7:33
Your code works for me. Could you show code with test cases?
– sawim
Nov 12 '18 at 7:33
Try with and operator ` if( (a == null && b == null) &&( a.equals("") && b.equals("")) )...`
– SaviNuclear
Nov 12 '18 at 7:34
Try with and operator ` if( (a == null && b == null) &&( a.equals("") && b.equals("")) )...`
– SaviNuclear
Nov 12 '18 at 7:34
@sawim I uploaded the complete code using a pastebin link. I don't have access to the failing test case (#8) because it's locked.
– RuchikaSingh
Nov 12 '18 at 7:41
@sawim I uploaded the complete code using a pastebin link. I don't have access to the failing test case (#8) because it's locked.
– RuchikaSingh
Nov 12 '18 at 7:41
What if a and b are having just whitespaces instead of empty string? I am guessing you should be writing your if statement as a == null || b == null || a.trim().equals("") || b.trim().equals("")
– Mohamed Anees A
Nov 12 '18 at 7:42
What if a and b are having just whitespaces instead of empty string? I am guessing you should be writing your if statement as a == null || b == null || a.trim().equals("") || b.trim().equals("")
– Mohamed Anees A
Nov 12 '18 at 7:42
add a comment |
1 Answer
1
active
oldest
votes
I see that you don't pass one test case, and it does not involve invalid inputs. You've missed one thing, you should take into consideration case sensitivity
a = a.toLowerCase();
b = b.toLowerCase();
Just make your strings lowercase after validation and it should be alright
I'm already converting the input to lowercase when I add it to the HashMap (lines 10-11 on pastebin), do I need to do it separately too?
– RuchikaSingh
Nov 12 '18 at 23:02
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%2f53257544%2fchecking-for-empty-string-using-equals-isempty-and-length-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I see that you don't pass one test case, and it does not involve invalid inputs. You've missed one thing, you should take into consideration case sensitivity
a = a.toLowerCase();
b = b.toLowerCase();
Just make your strings lowercase after validation and it should be alright
I'm already converting the input to lowercase when I add it to the HashMap (lines 10-11 on pastebin), do I need to do it separately too?
– RuchikaSingh
Nov 12 '18 at 23:02
add a comment |
I see that you don't pass one test case, and it does not involve invalid inputs. You've missed one thing, you should take into consideration case sensitivity
a = a.toLowerCase();
b = b.toLowerCase();
Just make your strings lowercase after validation and it should be alright
I'm already converting the input to lowercase when I add it to the HashMap (lines 10-11 on pastebin), do I need to do it separately too?
– RuchikaSingh
Nov 12 '18 at 23:02
add a comment |
I see that you don't pass one test case, and it does not involve invalid inputs. You've missed one thing, you should take into consideration case sensitivity
a = a.toLowerCase();
b = b.toLowerCase();
Just make your strings lowercase after validation and it should be alright
I see that you don't pass one test case, and it does not involve invalid inputs. You've missed one thing, you should take into consideration case sensitivity
a = a.toLowerCase();
b = b.toLowerCase();
Just make your strings lowercase after validation and it should be alright
answered Nov 12 '18 at 7:47
Schidu LucaSchidu Luca
3,042521
3,042521
I'm already converting the input to lowercase when I add it to the HashMap (lines 10-11 on pastebin), do I need to do it separately too?
– RuchikaSingh
Nov 12 '18 at 23:02
add a comment |
I'm already converting the input to lowercase when I add it to the HashMap (lines 10-11 on pastebin), do I need to do it separately too?
– RuchikaSingh
Nov 12 '18 at 23:02
I'm already converting the input to lowercase when I add it to the HashMap (lines 10-11 on pastebin), do I need to do it separately too?
– RuchikaSingh
Nov 12 '18 at 23:02
I'm already converting the input to lowercase when I add it to the HashMap (lines 10-11 on pastebin), do I need to do it separately too?
– RuchikaSingh
Nov 12 '18 at 23:02
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%2f53257544%2fchecking-for-empty-string-using-equals-isempty-and-length-in-java%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
Maybe the invalid input is something like two spaces. Then your if statement is not executed. in this case you have to trim the input first.
– tomas
Nov 12 '18 at 7:32
Your code works for me. Could you show code with test cases?
– sawim
Nov 12 '18 at 7:33
Try with and operator ` if( (a == null && b == null) &&( a.equals("") && b.equals("")) )...`
– SaviNuclear
Nov 12 '18 at 7:34
@sawim I uploaded the complete code using a pastebin link. I don't have access to the failing test case (#8) because it's locked.
– RuchikaSingh
Nov 12 '18 at 7:41
What if a and b are having just whitespaces instead of empty string? I am guessing you should be writing your if statement as a == null || b == null || a.trim().equals("") || b.trim().equals("")
– Mohamed Anees A
Nov 12 '18 at 7:42