Method to Check Password in Java Not Working










0















I'm trying to write a method that returns if the string is or isn't a valid password in CodeHS.



It needs to be at least eight characters long and can only have letters and digits.



In the grader, it passes every test except for passwordCheck("codingisawesome") and passwordCheck("QWERTYUIOP").



Here's what I have so far:



public boolean passwordCheck(String password)

if (password.length() < 8)

return false;

else

char c;
int count = 0;
for (int i = 0; i < password.length(); i++)

c = password.charAt(i);
if (!Character.isLetterOrDigit(c))

return false;
else if (Character.isDigit(c))

count++;


if (count < 2)

return false;


return true;



If anyone can help, I'd appreciate it. Thanks.










share|improve this question
























  • Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?

    – Elliott Frisch
    Nov 15 '18 at 3:22











  • You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.

    – Amadan
    Nov 15 '18 at 3:22












  • from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that

    – Kartik
    Nov 15 '18 at 3:39















0















I'm trying to write a method that returns if the string is or isn't a valid password in CodeHS.



It needs to be at least eight characters long and can only have letters and digits.



In the grader, it passes every test except for passwordCheck("codingisawesome") and passwordCheck("QWERTYUIOP").



Here's what I have so far:



public boolean passwordCheck(String password)

if (password.length() < 8)

return false;

else

char c;
int count = 0;
for (int i = 0; i < password.length(); i++)

c = password.charAt(i);
if (!Character.isLetterOrDigit(c))

return false;
else if (Character.isDigit(c))

count++;


if (count < 2)

return false;


return true;



If anyone can help, I'd appreciate it. Thanks.










share|improve this question
























  • Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?

    – Elliott Frisch
    Nov 15 '18 at 3:22











  • You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.

    – Amadan
    Nov 15 '18 at 3:22












  • from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that

    – Kartik
    Nov 15 '18 at 3:39













0












0








0








I'm trying to write a method that returns if the string is or isn't a valid password in CodeHS.



It needs to be at least eight characters long and can only have letters and digits.



In the grader, it passes every test except for passwordCheck("codingisawesome") and passwordCheck("QWERTYUIOP").



Here's what I have so far:



public boolean passwordCheck(String password)

if (password.length() < 8)

return false;

else

char c;
int count = 0;
for (int i = 0; i < password.length(); i++)

c = password.charAt(i);
if (!Character.isLetterOrDigit(c))

return false;
else if (Character.isDigit(c))

count++;


if (count < 2)

return false;


return true;



If anyone can help, I'd appreciate it. Thanks.










share|improve this question
















I'm trying to write a method that returns if the string is or isn't a valid password in CodeHS.



It needs to be at least eight characters long and can only have letters and digits.



In the grader, it passes every test except for passwordCheck("codingisawesome") and passwordCheck("QWERTYUIOP").



Here's what I have so far:



public boolean passwordCheck(String password)

if (password.length() < 8)

return false;

else

char c;
int count = 0;
for (int i = 0; i < password.length(); i++)

c = password.charAt(i);
if (!Character.isLetterOrDigit(c))

return false;
else if (Character.isDigit(c))

count++;


if (count < 2)

return false;


return true;



If anyone can help, I'd appreciate it. Thanks.







java passwords






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 4:23









Ketan Yekale

1,27421323




1,27421323










asked Nov 15 '18 at 3:19









Bob SmithBob Smith

12




12












  • Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?

    – Elliott Frisch
    Nov 15 '18 at 3:22











  • You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.

    – Amadan
    Nov 15 '18 at 3:22












  • from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that

    – Kartik
    Nov 15 '18 at 3:39

















  • Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?

    – Elliott Frisch
    Nov 15 '18 at 3:22











  • You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.

    – Amadan
    Nov 15 '18 at 3:22












  • from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that

    – Kartik
    Nov 15 '18 at 3:39
















Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?

– Elliott Frisch
Nov 15 '18 at 3:22





Are you certain about those requirements? Nothing about uppercase and lowercase letter(s)?

– Elliott Frisch
Nov 15 '18 at 3:22













You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.

– Amadan
Nov 15 '18 at 3:22






You are checking if password has at least two digits. "It needs to be at least eight characters long and can only have letters and digits" has no such requirement. By this specification, both of those passwords should be valid, but you are refusing them because they don't have enough digits for your taste.

– Amadan
Nov 15 '18 at 3:22














from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that

– Kartik
Nov 15 '18 at 3:39





from your code it looks like you have a requirement of having at least 2 digits, but you haven't mentioned that

– Kartik
Nov 15 '18 at 3:39












3 Answers
3






active

oldest

votes


















1














Assuming your requirement is as stated




It needs to be at least eight characters long and can only have letters and digits




Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,



public boolean passwordCheck(String password) 
if (password != null && password.length() >= 8)
for (char ch : password.toCharArray())
if (!Character.isLetterOrDigit(ch))
return false;


return true;

return false;






share|improve this answer






























    3














    Try an approach using patterns (this is simpler than looping):



    public boolean passwordCheck(String password)

    return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");



    Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html






    share|improve this answer

























    • @Kartik True, I added it.

      – Friwi
      Nov 15 '18 at 3:39


















    1














    It's failing those tests because your code checks that the password must have at least 2 digits:-




    if (count < 2)

    return false;




    And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.






    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%2f53311915%2fmethod-to-check-password-in-java-not-working%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Assuming your requirement is as stated




      It needs to be at least eight characters long and can only have letters and digits




      Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,



      public boolean passwordCheck(String password) 
      if (password != null && password.length() >= 8)
      for (char ch : password.toCharArray())
      if (!Character.isLetterOrDigit(ch))
      return false;


      return true;

      return false;






      share|improve this answer



























        1














        Assuming your requirement is as stated




        It needs to be at least eight characters long and can only have letters and digits




        Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,



        public boolean passwordCheck(String password) 
        if (password != null && password.length() >= 8)
        for (char ch : password.toCharArray())
        if (!Character.isLetterOrDigit(ch))
        return false;


        return true;

        return false;






        share|improve this answer

























          1












          1








          1







          Assuming your requirement is as stated




          It needs to be at least eight characters long and can only have letters and digits




          Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,



          public boolean passwordCheck(String password) 
          if (password != null && password.length() >= 8)
          for (char ch : password.toCharArray())
          if (!Character.isLetterOrDigit(ch))
          return false;


          return true;

          return false;






          share|improve this answer













          Assuming your requirement is as stated




          It needs to be at least eight characters long and can only have letters and digits




          Then there is no need to count digits. Simply check that the password is the minimum length, then loop over every character returning false if any are not a letter or digit. Like,



          public boolean passwordCheck(String password) 
          if (password != null && password.length() >= 8)
          for (char ch : password.toCharArray())
          if (!Character.isLetterOrDigit(ch))
          return false;


          return true;

          return false;







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 3:29









          Elliott FrischElliott Frisch

          155k1393187




          155k1393187























              3














              Try an approach using patterns (this is simpler than looping):



              public boolean passwordCheck(String password)

              return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");



              Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html






              share|improve this answer

























              • @Kartik True, I added it.

                – Friwi
                Nov 15 '18 at 3:39















              3














              Try an approach using patterns (this is simpler than looping):



              public boolean passwordCheck(String password)

              return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");



              Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html






              share|improve this answer

























              • @Kartik True, I added it.

                – Friwi
                Nov 15 '18 at 3:39













              3












              3








              3







              Try an approach using patterns (this is simpler than looping):



              public boolean passwordCheck(String password)

              return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");



              Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html






              share|improve this answer















              Try an approach using patterns (this is simpler than looping):



              public boolean passwordCheck(String password)

              return password!=null && password.length()>=8 && password.matches("[A-Za-z0-9]*");



              Decent tutorial on regular expressions (that's where the A-Z magic comes from): http://www.vogella.com/tutorials/JavaRegularExpressions/article.html







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 3:39

























              answered Nov 15 '18 at 3:31









              FriwiFriwi

              422212




              422212












              • @Kartik True, I added it.

                – Friwi
                Nov 15 '18 at 3:39

















              • @Kartik True, I added it.

                – Friwi
                Nov 15 '18 at 3:39
















              @Kartik True, I added it.

              – Friwi
              Nov 15 '18 at 3:39





              @Kartik True, I added it.

              – Friwi
              Nov 15 '18 at 3:39











              1














              It's failing those tests because your code checks that the password must have at least 2 digits:-




              if (count < 2)

              return false;




              And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.






              share|improve this answer



























                1














                It's failing those tests because your code checks that the password must have at least 2 digits:-




                if (count < 2)

                return false;




                And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.






                share|improve this answer

























                  1












                  1








                  1







                  It's failing those tests because your code checks that the password must have at least 2 digits:-




                  if (count < 2)

                  return false;




                  And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.






                  share|improve this answer













                  It's failing those tests because your code checks that the password must have at least 2 digits:-




                  if (count < 2)

                  return false;




                  And your test strings don't have any. Remove this piece of code and it should work. For a better way of doing it, see other answers.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 3:42









                  KartikKartik

                  4,00231437




                  4,00231437



























                      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%2f53311915%2fmethod-to-check-password-in-java-not-working%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







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3