Wednesday, 15 July 2015

Java Programs Made Easy

Q)...validate a password 
i) there should be atleast one digit
ii) there should be atleast one of '#','@' or '$' .
iii)the password should be of 6 to 20 characters
iv) there should be more uppercase letter than lower case.
v) should start with an upper case and end with lower case
 


  1. package Set1;
  2. import java.util.*;
  3. public class ClassSet33 {
  4. public static void main(String[] args) {
  5.   Scanner s=new Scanner(System.in);
  6.   String st=s.next();
  7.   boolean b=validatingPassword(st);
  8.   if(b==true)
  9.    System.out.println("valid password");
  10.   else
  11.    System.out.println("Invalid Password");
  12.  }
  13. public static boolean validatingPassword(String st) {
  14.  boolean b1=false,b2=false;
  15.  if(Character.isUpperCase(st.charAt(0)))
  16.   if(Character.isLowerCase(st.charAt(st.length()-1)))
  17.    if(st.length()>=6 && st.length()<=20)
  18.     for (int i = 0; i < st.length();i++)   
  19.        {   char c = st.charAt(i);   
  20.            if(Character.isDigit(c)){   
  21.              b1=true; break; }     }
  22.  int x=0,y=0;
  23.  for(int i = 0; i < st.length(); i++)
  24.        if(Character.isUpperCase(st.charAt(i)))
  25.          x++;
  26.        else if(Character.isLowerCase(st.charAt(i)))
  27.       y++;
  28.  if(b1==true)
  29.   if(x>y)
  30.    for (int i = 0; i < st.length();i++)   
  31.       {   char c = st.charAt(i);   
  32.           if(c=='#' || c=='@' || c=='$'){   
  33.             b2=true; break; }     }
  34.  return b2;
  35. }
  36. }