I successfully compile the code through cmd, however when i run it,
when i enter my password with anything(which fulfill the first condition (6-20))it will show error.
The question is to write a program that let user to create a password which is valid (more than 6 letters, must have atleast 1 uppercase,lowercase and digit)
Sorry if my post violent any rule(this is my first post !!)
p/s: i am quite new to java....
import java.util.Scanner;
public class Amazon {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
User Account = new User();
System.out.println("Account No: ");
int userID = input.nextInt();
System.out.println("Enter your desired password: ");
String Password = Account.readPassword();
}
}
import java.util.Scanner;
public class User {
static Scanner input = new Scanner(System.in);
String Password;
User() {
}
public String readPassword() {
String newPassword = input.nextLine();
while (newPassword.length() < 6 | newPassword.length() > 20) {
System.out.println("Password length is between 6 - 20 characters.\nPassword: ");
newPassword = input.nextLine();
}
boolean testLower = checkLower(newPassword);
boolean testCapitalDigit = checkCapitalDigit(newPassword);
while (testLower != true | testCapitalDigit != true ) {
System.out.println("Invalid Password.Please enter a valid password.\nPassword: ");
newPassword = input.nextLine();
}
return newPassword;
}
public boolean checkCapitalDigit(String newPassword) {
int Count = 0;
for (int i = 0; i <= newPassword.length();i++) {
if(Character.isUpperCase(newPassword.charAt(i)) && Character.isDigit(newPassword.charAt(i))) {
Count++;
}
}
boolean checkUpperDigit = (Count>0) ;
return checkUpperDigit;
}
public boolean checkLower(String newPassword) {
int Count = 0;
String lowerCase = newPassword.toLowerCase();
for (int i = 0; i <= newPassword.length(); i++) {
if(lowerCase.charAt(i) == newPassword.charAt(i)) {
Count++;
}
}
boolean checkLower = (Count>0);
return checkLower;
}
}