Java Reference
In-Depth Information
198
// Verifies password has proper format
199
private void verifyFormat ( String pswd ) throws Exception
200
{
201
boolean numFound = false ;
202
203
if ( pswd.length () == 0 )
204
throw new Exception ( "No password provided!" ) ;
205
206
if ( pswd.length () < MIN_SIZE )
207
throw new Exception ( "Password must be at least " +MIN_SIZE+
" characters in length" ) ;
208
209
if ( pswd.length () > MAX_SIZE )
210
throw new Exception ( "Password cannot be greater than " +MAX_SIZE+
" characters in length" ) ;
211
212
// scan through password to find if at least 1 number is used
213
for ( int i=0; i < pswd.length () && !numFound; ++i )
214
if ( Character .isDigit ( pswd.charAt ( i )))
215
numFound = true ;
216
217
if ( !numFound )
218
throw new Exception ( "Password is invalid - must have at least one numeric
digit" ) ;
219
}
FIGURE 9-36
verify, and an exception is generated with the message, No password provided! It
may be that no password value is supplied, even though this method is used only
within the class. If the validate() or set() methods receive a null string, or one
with only white space characters which subsequently are trimmed, then no pass-
word value is available to verify. If, at some later time, the verifyFormat()
method were made public, so as to provide users with the ability to verify the
format of potential passwords before submitting them, then the possibility of a
null string would only increase. In either case, it is better to check for and handle
this potential exception condition.
Lines 206 through 210 of Figure 9-36 verify that the password value is between
the minimum and maximum lengths. If the password has fewer characters than
the value specified as the value for MIN_SIZE (in this case, 6 characters), then the
exception is thrown with the error message, Password must be at least 6 characters
in length. If the password is longer than the value specified as the value for
MAX_SIZE (in this case, 15 characters), then the exception is thrown with the
error message, Password cannot be greater than 15 characters in length.
The final requirement for a valid password format is that it contains at least
one numeric character, and this is implemented by the code in lines 213 through
215. The charAt() method of the String class (line 214) provides the character
at a given index within the character sequence of the string. By iterating through
the characters in the string, each character can be examined one at a time to see
if it is numeric. The loop needs to continue only until all characters have been
examined or a numeric character is found. The charAt() method, as it returns
the character value at the given index, becomes the argument of the isDigit()
method to determine if a numeric character exists. The isDigit() method , a
static method of the Character class, passes the character being examined and
returns true if the character is a digit, or number (line 214). If none of the charac-
ters in the password is numeric, an exception is thrown with the error message,
Password is invalid — must have at least one numeric digit (lines 217 and 218).
Search WWH ::




Custom Search