0

Ask an user to type in a password, a qualified password should follow the constraints, such as:
Length of password >= 8
Include at least one uppercase character
Include at least one lowercase character
Include at least one integer
Include at least special character
Write a program to check if the user gives a qualified password.

I dont think should be using so many while statments, when i run and enter the characters im not getting the quilified password, help plz.

import javax.swing.JOptionPane;

public class PassWord
{
    public static void main (String[] args)
    {
         String input;


      input = JOptionPane.showInputDialog("Enter " +
         "a password in the form Ln1@shl2\n");


      if (isValid(input))
      {
         JOptionPane.showMessageDialog(null,
                 "That's a qualified password.");
      }
      else
      {
         JOptionPane.showMessageDialog(null,
             "That is not the proper format of a " +
             "password, it should include at least one uppercase & lowercase letter, one number & a special charater .\nHere is an " +
             "example: Ab$dj234");
      }

      System.exit(0);
   }



   private static boolean isValid(String passUser)
   {
      boolean goodSoFar = true;
      int i = 0;


      if (passUser.length() != 8)
         goodSoFar = false;


      while (goodSoFar && i < 1)
      {
         if (!Character.isLetter(passUser.charAt(i)))
            goodSoFar = false;
        i++;
      }


      while (goodSoFar && i < 2)
      {
         if (!Character.isDigit(passUser.charAt(i)))
            goodSoFar = false;
        i++;
      }

      while (goodSoFar && i < 3)
      {
         if (!Character.isUpperCase(passUser.charAt(i)))
            goodSoFar = false;
        i++;
      }

      while (goodSoFar && i < 4)
      {
         if (!Character.isLowerCase(passUser.charAt(i)))
            goodSoFar = false;
        i++;
      }

      while (goodSoFar && i < 5)
      {
         if (!Character.isSpaceChar(passUser.charAt(i)))
            goodSoFar = false;
        i++;
      }

      while (goodSoFar && i < 8)
      {
         if (!Character.isLetter(passUser.charAt(i)))
            goodSoFar = false;
         i++;
      }

      return goodSoFar;
    }
}
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by Taywin
0

A very simple solution is to use regular expression with matches() method of String class. You may need to read about what it is. It is not difficult, and you should try to understand it. It will be very helpful to you in the future when you deal with String. Anyway, below is how you use it to simplify your program.

// Assume that the password string is in a variable named passwdStr
// Length of password >= 8
// You know how to check the string length, right?

//Include at least one uppercase character
if (!passwdStr.matches(".*[A-Z].*")) {
  // the password string does NOT contain at least capital letter
}

//Include at least one lowercase character
if (!passwdStr.matches(".*[a-z].*")) {
  // the password string does NOT contain at least one lower case letter
}

//Include at least one integer
if (!passwdStr.matches(".*\\d.*")) {
  // the password string does NOT contain at least one digit number
}

//Include at least special character
// What are the list of special character???
// My guess here are !@#%_-*$^
if (!passwdStr.matches(".*[!@#%&_-\*\$\^].*")) {
  // the password string does NOT contain at least one special character
}

Edited by Taywin

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.