Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help with program that checks requirements

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Need help with program that checks requirements

    Hi there,
    I need to write a program that uses a class to check requirements of a password. The password needs to be at least 6 characters in length, contain 1 uppercase, 1 lowercase, and 1 digit. I wrote something, and I swore it worked at one point in time but now it's not working and I don't know what the issue could be. Any advice would be appreciated. Thanks

    My logic is that:

    1) It checks if the minimum length is acceptable. If not, it automatically results in asking for password again.
    2) Then it if you have an upper case, the count goes up. And if the countU > 0 then it is okay. If the countU = 0, it kicks back and makes you enter password again.
    3) It will do this for lowercase, and digits as well. Making sure that you need at least 6 characters, 1 uppercase, 1 lower case, and 1 digit.

    However, my program doesn't work. It accepts weird passwords and kicks back acceptable passwords.

    For example:

    It denies this password, even though it fits the requirements of at least 6 characters, 1 uppercase, 1 lowercase, and 1 digit.
    Enter a Password:
    sadfASD123


    and then it accepts this password! Even though it doesn't fit the requirement.
    Enter a Password:
    ASDASDASDASD

    So this makes me believe there is something very wrong with how I wrote the for loops.



          public static boolean okPassword(String testPW)
     
          {
             int len = testPW.length();
             boolean PassOK = true;
     
             int upC, countU = 0;
             int lowC, countL = 0;
             int digC, countD = 0;
     
     
             if (len >= 6)
             {
     
     
                for (upC = 0; upC < testPW.length(); upC++)
                {
                   if (Character.isUpperCase(testPW.charAt(upC)))
                   {
                      countU++;
                      PassOK = true;
                      return PassOK;
                   }
     
                   else
     
                      if (countU == 0)
                      {
                         PassOK = false;
                         return PassOK;
                      }
                }
     
                for(digC = 0; digC < testPW.length(); digC++)
                {
                   if (Character.isDigit(testPW.charAt(digC)))
                   {            
                      countD++;
                      PassOK = true;
                      return PassOK;
                   }
                   else
     
                      if (countD == 0)
                      {
                         PassOK = false;
                         return PassOK;
                      }
                }
     
                for(lowC = 0; lowC < testPW.length(); lowC++)
                {
                   if (Character.isLowerCase(testPW.charAt(lowC)))
                      {
    						countL++;
    						PassOK = true;
    						return PassOK;
    						}
     
                   else
     
                      if (countL == 0)
                      {
                         PassOK = false;
                         return PassOK;
                      }
                }
     
     
     
             }
     
             else PassOK = false;
             return PassOK;
     
          }
    Last edited by suxen; March 28th, 2011 at 05:25 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help with program that checks requirements

    im not sure but try to use pater
    for enter password use
    boolean b=true;
    while(b){ ... 
    if (pass.leght>6; b=false;)
    }

    now make 3 patern

    Pattern p = Pattern.compile(myMagicPattern); // insert your pattern here
    Matcher m = p.matcher(contentString);
    if (m.find()) 3 times pass is ok

    patern for lower characters [a-z] , upper [A-Z] , digits [0-9] ; lol 1 pattern [a-zA-Z0-9]
    just try it

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with program that checks requirements

    I think I fixed it.

    I don't know what I was doing with all those returns in the first code. I cleaned it up a bit, and it seems to work!

    Also, thank you Bojan for the suggestion. However, I needed to do it this way.


        public static boolean okPassword(String testPW)
     
          {
             int len = testPW.length();
             boolean PassOK = true;
     
     
             int upC, countU = 0;
             int lowC, countL = 0;
             int digC, countD = 0;
     
     
             if (len >= 6)
             {
     
     
                for (len = 0; len < testPW.length(); len++)
                {
                   if (Character.isUpperCase(testPW.charAt(len)))
                   {
                      countU++;
                   }
     
                   else
     
     
                      if (Character.isLowerCase(testPW.charAt(len)))
                      {
                        countL++;
                      }
     
    					else
     
    						if (Character.isDigit(testPW.charAt(len)))
    						{
    							countD++;
    						}
                }
     
    				if ((countU > 0) && (countL > 0) && (countD > 0))
    					{
    					PassOK = true;
    					}
     
    				else
     
    					PassOK = false;
     
     
     
             }
     
             else PassOK = false;
             return PassOK;
     
          }

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with program that checks requirements

    Actually, you were putting your else if(UC==0) return something.
    This will definitely behave very different as your count is initialized to 0 and whenever it'll not find first upper case it'll throw your code out.
    So, instead of writing else there, put it outside the loop and check out there as you have done in your last post. Anyways, good luck and i read this late.

  5. The Following User Says Thank You to Mr.777 For This Useful Post:

    suxen (March 29th, 2011)

  6. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with program that checks requirements

    Thanks Mr. 777, I get what you're saying!

Similar Threads

  1. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM