-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06 (Java) Check whether a string is a valid password.java
More file actions
62 lines (48 loc) · 1.74 KB
/
06 (Java) Check whether a string is a valid password.java
File metadata and controls
62 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package BasicCode;
import java.util.Scanner;
public class BasicCode {
public static final int PASSWORD_LENGTH = 8;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(
"1. A password must have at least eight characters.\n" +
"2. A password must contain at least two digits \n" +
"------ ------ ------ ------ ------ ------ \n"+
"Enter a password: ");
String s = input.nextLine();
if(is_Valid_Password(s)){
System.out.println("Password is valid: " + s);
}
else{
System.out.println("Not a valid password: " + s);
}
}
public static boolean is_Valid_Password(String password){
if (password.length() < PASSWORD_LENGTH){
return false;
}
int charCount = 0;
int numCount = 0;
for (int i = 0; i < password.length(); i++){
char ch = password.charAt(i);
if (is_Numeric(ch)) numCount++;
else if (is_Letter(ch)) charCount++;
else return false;
}
return (charCount >= 2 && numCount >= 2);
}
public static boolean is_Letter(char ch){
ch = Character.toUpperCase(ch);
return (ch >= 'A' && ch <= 'Z');
}
public static boolean is_Numeric(char ch) {
return (ch >= '0' && ch <= '9');
}
}
/* ----- Output ----
1. A password must have at least eight characters.
2. A password must contain at least two digits
------ ------ ------ ------ ------ ------
Input: Enter a password: YahooMail2019
Result: Password is valid: YahooMail2019
*/