Advertisement
Kancho

Password_Validator

Mar 29th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Password_Validator {
  6. public static void main(String[] args) throws IOException {
  7.  
  8. BufferedReader reader =
  9. new BufferedReader(
  10. new InputStreamReader(System.in));
  11.  
  12. System.out.print("Enter password: ");
  13. String password = reader.readLine();
  14.  
  15. if(!hasSixToTenDigits(password)) {
  16. System.out.println("The password mast be between six and ten symbols!");
  17. }
  18. if (!hasOnlyLettersAndDigits(password)) {
  19. System.out.println("The password must consists of letters and digits, only!");
  20. }
  21. if (!hasAtLeastTwoDigits(password)) {
  22. System.out.println("The password must have at least two digits!");
  23. }
  24. if(hasAtLeastTwoDigits(password) && hasOnlyLettersAndDigits(password) &&
  25. hasSixToTenDigits(password)){
  26. System.out.println("Valid Password!");
  27. }
  28.  
  29. }
  30.  
  31. private static boolean hasSixToTenDigits(String password) {
  32. if (password.length() >= 6 && password.length() <= 10) {
  33. return true;
  34. }
  35. return false;
  36. }
  37.  
  38. private static boolean hasOnlyLettersAndDigits(String password) {
  39.  
  40. for (int i = 0; i < password.length(); i++) {
  41.  
  42. if (!Character.isLetterOrDigit(password.charAt(i))) {
  43. return false;
  44. }
  45.  
  46. }
  47.  
  48. return true;
  49. }
  50.  
  51. private static boolean hasAtLeastTwoDigits(String password) {
  52.  
  53. int count = 0;
  54. for (int i = 0; i < password.length(); i++) {
  55. if (Character.isDigit(password.charAt(i))) {
  56. count ++;
  57. }
  58. }
  59. if (count < 2){
  60. return false;
  61. }
  62. return true;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement