简体   繁体   中英

Making a password verification into a class

I have a program grabbing a password from the user, then it checks if the conditions are met or not then outputs "Valid Password" or "Invalid Password". This works, and I was able to turn the verification aspect into a method in the same program and it works, but I want to make it into a class where I can just say if( validate(pw) == true ) ... or at least if( v.getValidation() == true ) ... in any program and it will test my conditions. I've used custom classes before but for some reason everything I try does not work on this one, I've been at it for days.

Here's my method:

public boolean validate( String pw )
{
   boolean l = false, u = false, lo = false, d = false, r = true;

   if( pw.length() >= 6 )
     { l = true; }      

   for( int i = 0; i < pw.length(); i++ )
   {
      if( Character.isUpperCase( pw.charAt(i) ) )
       { u = true; }

      if( Character.isLowerCase( pw.charAt(i) ) )
       { lo = true; }

      if( Character.isDigit( pw.charAt(i) ) )
       { d = true; }
   }

   if( l == false || u == false || lo == false || d == false )
     { r = false; }

   return r;
}

Edit:

Thank you all for your input, this is what it came out to in the end:

public class Password
{
   public static boolean validate( String pw )
   {
      boolean result = false;
      int upper = 0, lower = 0, digit = 0;

      if( pw.length() >= 6 )
      {
         for( int i = 0; i < pw.length(); i++ )
         {
            if( Character.isUpperCase( pw.charAt(i) ) )
              { upper++; }

            if( Character.isLowerCase( pw.charAt(i) ) )
              { lower++; }

            if( Character.isDigit( pw.charAt(i) ) )
              { digit++; }
         }
      }

      if( upper >= 1 && lower >= 1 && digit >= 1 )
        { result = true; }

      return result;
   }
}

You do not need to make a whole class for this. You can do something like:

public static void main(String[] args) {
    boolean valid = validate("PassWord22");
}
public static boolean validate( String pw ) {}

Also some notes on your method:

  • You don't need to do l == true or l == false in your if statement. You can simply do:

     if( !l || !u || !lo || !d ) { r = false; } 

    In fact you can just return

     return l && u && lo && d; 
  • If the length is not 6 or greater, simply return false. This will save checking all the letters in the String

  • I would come up with better variable names. Single/two letter variable names makes it very hard to tell what they represent, and easy to mix up. (instead of l you could have length and instead of u you could have upper )

Also this can be easier solved with regex and String.matches() :

public static boolean validate(String pw) {
    String pattern = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).+$";
    return (pw.length() > 5 && pw.matches(pattern));
}

I got it working, turned out to be an error in the code that the compiler was not picking up. I wanted to delete the question but they wont let me for some reason. So in case you're curious this is what my class looks like functioning:

public class Password
{
   private String pw;

   public Password()
   {
      pw = "";
   }

   public Password( String pw )
   {
      this.pw = pw;
   }

   public boolean getPassword( String pw )
   {
      boolean l = false, u = false, lo = false, d = false, r = true;

      if( pw.length() >= 6 )
        { l = true; }      

      for( int i = 0; i < pw.length(); i++ )
      {
         if( Character.isUpperCase( pw.charAt(i) ) )
           { u = true; }

         if( Character.isLowerCase( pw.charAt(i) ) )
           { lo = true; }

         if( Character.isDigit( pw.charAt(i) ) )
           { d = true; }
      }

      if( l == false || u == false || lo == false || d == false )
        { r = false; }

      return r;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM