Checking for empty string using .equals(), .isEmpty() and .length() in Java










1















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










share|improve this question
























  • 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















1















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










share|improve this question
























  • 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













1












1








1








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer























  • 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










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









0














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






share|improve this answer























  • 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















0














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






share|improve this answer























  • 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













0












0








0







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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



















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%2f53257544%2fchecking-for-empty-string-using-equals-isempty-and-length-in-java%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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

PHP code is not being executed, instead code shows on the page

Administrative divisions of China