Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Main {
  3.  
  4. static String checkLength(String input) {
  5. int length = 0;
  6. for (int i = 0; i < input.length(); i++) {
  7. length++;
  8. }
  9. if (length > 5 && length < 11) {
  10. return "valid";
  11. } else {
  12. System.out.println("Password must be between 6 and 10 characters");
  13. return "not valid";
  14. }
  15. }
  16.  
  17. static String checkLettersAndDigits (String input) {
  18. for (int i = 0; i < input.length() - 1; i++) {
  19. char symbol = input.charAt(i);
  20. if (!(symbol > 47 && symbol < 58) && !(symbol > 64 && symbol < 91) && !(symbol > 96 && symbol < 123)) {
  21. System.out.println("Password must consist only of letters and digits");
  22. return "not valid";
  23. }
  24. }
  25. return "valid";
  26. }
  27.  
  28. static String checkForDigits (String input) {
  29. int counter = 0;
  30. for (int i = 0; i < input.length() - 1; i++) {
  31. char symbol = input.charAt(i);
  32. if (symbol > 47 && symbol < 58) {
  33. counter++;
  34. }
  35. }
  36. if (counter >= 2) {
  37. return "valid";
  38. }
  39. System.out.println("Password must have at least 2 digits");
  40. return "not valid";
  41. }
  42.  
  43. public static void main(String[] args) {
  44.  
  45. Scanner scanner = new Scanner(System.in);
  46.  
  47. String input = scanner.nextLine();
  48.  
  49. checkLength(input);
  50. checkLettersAndDigits(input);
  51. checkForDigits(input);
  52.  
  53. if (checkLength(input).equals("valid") && checkLettersAndDigits(input).equals("valid") && checkForDigits(input)
  54. .equals("valid")) {
  55. System.out.println("Password is valid");
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement