Advertisement
mirozspace

Check password - v2

Feb 20th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Pr4PasswordValidator {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. String password = scanner.nextLine();
  7. if (isInRange(password) && isLetterOrDigit(password)
  8. && hasLeastTwoDigit(password)) {
  9. System.out.println("Password is valid");
  10. }
  11. if (!isInRange(password)) {
  12. System.out.println("Password must be between 6 and 10 characters");
  13. }
  14. if (!isLetterOrDigit(password)) {
  15. System.out.println("Password must consist only of letters and digits");
  16. }
  17. if (!hasLeastTwoDigit(password)) {
  18. System.out.println("Password must have at least 2 digits");
  19. }
  20. }
  21.  
  22. private static boolean isInRange(String password) {
  23. if (password.length() >= 6 && password.length() <= 10) {
  24. return true;
  25. }
  26. return false;
  27. }
  28.  
  29. private static boolean isLetterOrDigit(String password) {
  30. for (int i = 0; i < password.length(); i++) {
  31. if (!Character.isLetterOrDigit(password.charAt(i))) {
  32. return false;
  33. }
  34. }
  35. return true;
  36. }
  37.  
  38. private static boolean hasLeastTwoDigit(String password) {
  39. int countDigit = 0;
  40. for (int i = 0; i < password.length(); i++) {
  41. if (Character.isDigit(password.charAt(i))) {
  42. countDigit++;
  43. }
  44. }
  45. if (countDigit < 2) {
  46. return false;
  47. }
  48. return true;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement