1
+ package com .codefortomorrow .intermediate .chapter11 .solutions ;
2
+
3
+ import java .util .Scanner ;
4
+
5
+ /*
6
+ Some websites impose certain rules for passwords.
7
+ Write a method that checks whether a string is a valid password.
8
+
9
+ Suppose the password rules are as follows:
10
+ ■ A password must have at least eight characters.
11
+ ■ A password consists of only letters and digits.
12
+ ■ A password must contain at least two digits.
13
+
14
+ Write a program that prompts the user to enter a password and displays
15
+ Valid Password if the rules are followed or Invalid Password otherwise.
16
+ To do this, use a method called isValid which returns true if the password
17
+ is valid and false otherwise.
18
+
19
+ You may also find it helpful to write a method called
20
+ isAlphanumeric which returns true if the given string
21
+ is only made up of letters and digits.
22
+
23
+ You may also find it helpful to write a method called
24
+ getNumberOfDigits which returns the number of digits
25
+ in a given string.
26
+
27
+ You may also find it helpful to use the Character.isDigit(char)
28
+ and Character.isDigit(char) methods. You can read more about them here:
29
+ https://www.tutorialspoint.com/java/java_characters.htm
30
+
31
+ If you don't want to use those methods, you can also use regular
32
+ expressions (aka regex) and the matches(regex) String method.
33
+ You can read about that here:
34
+ https://www.tutorialspoint.com/java/java_string_matches.htm
35
+ https://www.vogella.com/tutorials/JavaRegularExpressions/article.html
36
+
37
+ Adapted from Exercise 6.18, Introduction to Java Programming (Comprehensive),
38
+ 10th ed. by Y. Daniel Liang
39
+ */
40
+
41
+ public class CheckPassword {
42
+ public static void main (String [] args ) {
43
+ // prompt user for a password
44
+ Scanner input = new Scanner (System .in );
45
+ System .out .print ("Password: " );
46
+ String password = input .nextLine ();
47
+ input .close ();
48
+
49
+ // print whether it's valid
50
+ if (isValid (password )) {
51
+ System .out .println ("Valid Password" );
52
+ } else {
53
+ System .out .println ("Invalid Password" );
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Check if a password is valid.
59
+ * A valid password must:
60
+ * - Contain at least 8 characters
61
+ * - Be alphanumeric
62
+ * - Contain at least 2 digits
63
+ * @param password password to check if valid
64
+ * @return true if the password is valid
65
+ */
66
+ public static boolean isValid (String password ) {
67
+ boolean atLeastEightChars = password .length () >= 8 ;
68
+ boolean isAlphanumeric = isAlphanumeric (password );
69
+ boolean atLeastTwoDigits = getNumberOfDigits (password ) >= 2 ;
70
+
71
+ return atLeastEightChars && isAlphanumeric && atLeastTwoDigits ;
72
+ }
73
+
74
+ /**
75
+ * Check if a String is alphanumeric
76
+ * (contains only letters and numbers)
77
+ * @param str String to check if alphanumeric
78
+ * @return true if the String is alphanumeric
79
+ */
80
+ public static boolean isAlphanumeric (String str ) {
81
+ for (int i = 0 ; i < str .length (); i ++) {
82
+ boolean isDigit = Character .isDigit (str .charAt (i ));
83
+ boolean isLetter = Character .isLetter (str .charAt (i ));
84
+ if (!isDigit && !isLetter ) {
85
+ return false ;
86
+ }
87
+ }
88
+ return true ;
89
+
90
+ // alternate solution using regular expressions
91
+ // matches lowercase and uppercase letters, and digits
92
+ // the + indicates the pattern must match 1 or more times
93
+ // (need to check each char in the string)
94
+ // return str.matches("[a-zA-Z0-9]+");
95
+ }
96
+
97
+ /**
98
+ * Returns the number of digits
99
+ * contained in the given String
100
+ * @param str String to get number of digits
101
+ * @return the number of digits contained in the given String
102
+ */
103
+ public static int getNumberOfDigits (String str ) {
104
+ int numberOfDigits = 0 ;
105
+ for (int i = 0 ; i < str .length (); i ++) {
106
+ if (Character .isDigit (str .charAt (i ))) {
107
+ numberOfDigits ++;
108
+ }
109
+ }
110
+ return numberOfDigits ;
111
+ }
112
+ }
0 commit comments