-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProb2.java
45 lines (41 loc) · 1.39 KB
/
Prob2.java
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
package lab4;
import java.util.Scanner;
public class Prob2 {
public static boolean isInt(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static void checkPass(String str) {
boolean ind = false;
int digits = 0;
if (str.length() >= 8) {
ind = true;
}
for (int i = 0; i < str.length(); i++) {
if (Character.isLetter(str.charAt(i)) == true)
ind = true;
else if (isInt(String.valueOf(str.charAt(i)))) {
digits++;
ind = true;
} else {
ind = false;
break;
}
}
if (ind && digits >= 2)
System.out.println("Valid Password");
else
System.out.println("Invalid Password");
}
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print(
"This program checks whether a string is a valid password. Suppose the password rules are as follows:\n- A password must have at least eight characters.\n- A password consists of only letters and digits.\n- A password must contain at least two digits.\nEnter your password: ");
String pass = inp.nextLine();
checkPass(pass);
}
}