import java.util.Scanner; //Some websites impose certain rules for passwords, //Write a java program that checks whether a string is valid passwords, //suppose the password rule is as follows, //A password must have at least eight characters, //A password consists of only letters and digits, //A password must contain at least two digits, //your program prompts the user to enter a password and displays"valid password", //if the rule is followed or"invalid password otherwise", public class Ok { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter the password:"); String password=input.next(); if(isLeastEightChara(password)&&isContainLeastTwoDigits(password)&&isOnlyLettersandDigits(password)) { System.out.println("Valid password"); } else { System.out.println("inValid password"); } } public static boolean isLeastEightChara(String password) { if(password.length()<=8) { return true; } else { return false; } }; public static boolean isContainLeastTwoDigits(String password) { int count=0; for(int i=0;i<password.length();i++) { char pass=password.charAt(i); if(Character.isDigit(pass)) { count++; } } if(count>=2) { return true; } else { return false; } }; public static boolean isOnlyLettersandDigits(String password){ boolean a=false; for(int i=0;i<password.length();i++){ char pass=password.charAt(i); if(Character.isDigit(pass)&&Character.isAlphabetic(pass)){ a=true; } return a; } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter