Advertisement
Valeri12580

Password checker

Feb 19th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PasswordValidator {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. String password = scanner.nextLine().toLowerCase();
  7. checker(password);
  8.  
  9.  
  10. }
  11.  
  12. static void checker(String password) {
  13. boolean failed = false;
  14. if (!(length(password))) {
  15. System.out.println("Password must be between 6 and 10 characters");
  16. failed = true;
  17. }
  18. if (!onlyLettersAndDigits(password)) {
  19. System.out.println("Password must consist only of letters and digits");
  20. failed = true;
  21. }
  22. if (!(isDigit(password))) {
  23. System.out.println("Password must have at least 2 digits");
  24. failed = true;
  25. }
  26.  
  27. if (!failed) {
  28. System.out.println("Password is valid");
  29. }
  30.  
  31.  
  32. }
  33.  
  34. static boolean length(String passord) {
  35. boolean isValid = false;
  36. if (passord.length() >= 6 && passord.length() <= 10) {
  37. isValid = true;
  38. }
  39. return isValid;
  40.  
  41. }
  42.  
  43. static boolean isDigit(String password) {
  44. boolean isValid = false;
  45. int counter = 0;
  46. for (int i = 0; i < password.length(); i++) {
  47. if (Character.isDigit(password.charAt(i))) {
  48. counter++;
  49. }
  50. }
  51.  
  52. if (counter >= 2) {
  53. isValid = true;
  54. }
  55.  
  56. return isValid;
  57. }
  58.  
  59. static boolean onlyLettersAndDigits(String password) {
  60. boolean isValidLettersAndValidDigits = true;
  61. for (int i = 0; i < password.length(); i++) {
  62. if (!(Character.isDigit(password.charAt(i)) ||Character.isAlphabetic(password.charAt(i)))) {
  63. isValidLettersAndValidDigits = false;
  64. break;
  65. }
  66. }
  67. if (isValidLettersAndValidDigits) {
  68. return true;
  69. } else {
  70. return false;
  71. }
  72.  
  73.  
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement