deiom

Untitled

Jun 12th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PasswordValidator {
  4. //Password Validator
  5. //Write a program that checks if a given password is valid. Password rules are:
  6. // 6 – 10 characters (inclusive);
  7. // Consists only of letters and digits;
  8. // Have at least 2 digits.
  9. //If a password is valid, print "Password is valid". If it is not valid, for every unfulfilled rule print a message:
  10. // "Password must be between 6 and 10 characters";
  11. // "Password must consist only of letters and digits";
  12. // "Password must have at least 2 digits"
  13. //Examples
  14. //Input
  15. //logIn
  16. // Output:
  17. // Password must be between 6 and 10 characters
  18. //Password must have at least 2 digits
  19. //Input:
  20. //MyPass123
  21. // Output:
  22. // Password is valid
  23. //Input
  24. //Pa$s$s
  25. // Output
  26. // Output:
  27. // Password must consist only of letters and digits
  28. //Password must have at least 2 digits
  29. //
  30. //Hints
  31. //Write a method for each rule.
  32. public static void main(String[] args) {
  33. Scanner scanner = new Scanner(System.in);
  34. String password = scanner.nextLine();
  35. boolean pass = false;
  36.  
  37. if (checkLength(password) && lettersOrNumbers(password) && checkForTwoDigits(password)) {
  38. System.out.println("Password is valid");
  39. } else if (!checkLength(password)) {
  40. System.out.println("Password must be between 6 and 10 character");
  41. } else if (!lettersOrNumbers(password)) {
  42. System.out.println("Password must consist only of letters and digits");
  43. } else if (!checkForTwoDigits(password)) {
  44. System.out.println("Password must have at least 2 digits");
  45. }
  46. }
  47.  
  48. public static boolean checkLength(String password) {
  49. for (int i = 0; i <= password.length(); i++) {
  50. if (i >= 5 && i <= 9) {
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58.  
  59. public static boolean lettersOrNumbers(String password) {
  60. for (int i = 0; i <= password.length(); i++) {
  61. char symbol = password.charAt(i);
  62. if (symbol >= 48 && symbol <= 57) {
  63. return true;
  64. } else if (symbol >= 65 && symbol <= 90) {
  65. return true;
  66. } else if (symbol >= 97 && symbol <= 122) {
  67. return true;
  68. } else {
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74.  
  75. public static boolean checkForTwoDigits(String password) {
  76. int counter = 0;
  77. for (int i = 0; i <= password.length(); i++) {
  78. char symbol = password.charAt(i);
  79. if (symbol >= 48 && symbol <= 57) {
  80. counter++;
  81. }
  82.  
  83. }
  84. if (counter >= 2) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment