Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check Password Program

 
Stephen Son
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic has been cross posted here.

Since I haven't been getting any replies on that topic, I figured I would post this same question on this site.

Some Websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rule is as follows:

  • A password must have at least 8 characters
  • A password consists of only letters and digits
  • A password must contain at least 2 digits


  • Write a program that prompts the user to enter a password and displays "Valid Password" if the rule is followed or "Invalid Password" otherwise.

    Here is my most up to date code:



    My code compiles and runs. The only part that isn't working is the part that checks to see if the password has at least 2 digits. How would I correct this?
     
    lokesh sree
    Ranch Hand
    Posts: 100
    Eclipse IDE Hibernate Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    The check on count of digits(i.e, count<2 ) should happen once you have scanned through all the characters of your password i.e, after the for loop.
    You can actually step through each line for yourself and see why it's not working.
     
    Stephen Son
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    lokesh chenta wrote:Hi,
    The check on count of digits(i.e, count<2 ) should happen once you have scanned through all the characters of your password i.e, after the for loop.
    You can actually step through each line for yourself and see why it's not working.

    Oh I see. Thanks. I fixed that in my code.
     
    Campbell Ritchie
    Sheriff
    Posts: 48454
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephen Son wrote:This topic has been cross posted here. . . .
    Welcome to the Ranch
    Thank you for telling us about the duplicate posting
    And you got an answer.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic