desislava_topuzakova

04. Password Validator

Oct 8th, 2021
1,540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. package Methods_Exercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PasswordValidator_04 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String password = scanner.nextLine();
  9.  
  10. //проверка за дължина -> [6; 10]
  11. boolean isPasswordValidLength = isValidLength(password); //true -> валидна; false -> не е валидна
  12. if (!isPasswordValidLength) {
  13. System.out.println("Password must be between 6 and 10 characters");
  14. }
  15. //проверка за съдържанието -> само букви и цифри
  16. boolean isValidPasswordContent = isValidContent(password); //true (валидна), false (невалидна)
  17. if (!isValidPasswordContent) {
  18. System.out.println("Password must consist only of letters and digits");
  19. }
  20.  
  21. //проверка за брой на цифирите -> минимум 2
  22. boolean isValidPasswordCountDigits = isValidCountDigits(password); //true (валидна), false (невалидна)
  23. if (!isValidPasswordCountDigits) {
  24. System.out.println("Password must have at least 2 digits");
  25. }
  26.  
  27. //валидна: isPasswordValidLength = true и isValidPasswordContent = true и isValidPasswordCountDigits = true
  28. if (isPasswordValidLength && isValidPasswordContent && isValidPasswordCountDigits) {
  29. System.out.println("Password is valid");
  30. }
  31.  
  32. }
  33.  
  34. //метод за валидиране на дължината -> true (валидна); false (невалидна)
  35. private static boolean isValidLength(String password) {
  36. //валидна -> дължина >= 6 и дължина <= 10
  37. /*if (password.length() >= 6 && password.length() <= 10) {
  38. return true;
  39. } else {
  40. return false;
  41. }*/
  42. return password.length() >= 6 && password.length() <= 10;
  43. }
  44.  
  45. //метод за валидиране на съдържанието -> true (валидна), false (невалидна)
  46. private static boolean isValidContent(String password) {
  47. //всички символи да са буква или цифра -> "T@st1234"
  48. //обхождаме всички символи
  49. //ако даден символ не е буква или цифра -> false
  50. for (char symbol : password.toCharArray()) {
  51. if (!Character.isLetterOrDigit(symbol)) {
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57.  
  58. //метод за валидиране на брой на цифрите -> валидна парола: брой >= 2
  59. private static boolean isValidCountDigits(String password) {
  60. //true -> count >= 2
  61. //false -> count < 2
  62. int count = 0; //брой на цифрите
  63. //обхождам всеки един символ -> проверка дали е цифра -> увеличавам броя
  64. for (char symbol : password.toCharArray()) {
  65. if (Character.isDigit(symbol)) {
  66. count++;
  67. }
  68. }
  69. //знам броя на цифрите
  70. /*if (count >= 2) {
  71. return true;
  72. } else {
  73. return false;
  74. }*/
  75. return count >= 2;
  76. }
  77.  
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment