The program I have been assigned has the following rules:
rules:
- number of chars must be in [6, 10]
2.there must be >= 2 chars in the range ['a', 'z']
3.there must be >= 2 chars in the range ['A', 'Z']
- there must be >= 2 chars in the range ['0', '9']
For example, pw1("aaBC09-+") should return true, and pw1("aabC09-+") should return false.
I have attached my code but is there a more efficient / cleaner code I could be using?
import java.util.*;
public class PassChecker2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt User to Enter String
System.out.print("Enter Password: ");
// Grab User Generated String
String pw = input.next();
// Pass String to be Checked
boolean valid_pw = checkPW(pw);
if(valid_pw)
System.out.println(pw + " is a valid password!");
else
System.out.println(pw + " is not a valid password!");
}
public static boolean checkPW(String pw)
{
int pwLowerCase = 0,
pwUpperCase = 0,
pwNumber = 0;
// Return False if PW is too Small / Large
if(!(pw.length() >= 2 && pw.length() <= 10))
return false;
for(int position = 0; position < pw.length(); ++position)
{
char character = pw.charAt(position);
if(character >= 'a' && character <= 'z')
++pwLowerCase;
else if(character >= 'A' && character <= 'Z')
++pwUpperCase;
else if(character >= '1' && character <= '9')
++pwNumber;
}
return (pwLowerCase >= 2 && pwUpperCase >= 2 && pwNumber >= 2);
}
}