
Jason N.
asked 12/10/20Password requirement
Websites commonly require a password that satisfies several requirements. Write a program that checks if an input string satisfies the following (error message is shown for each):
- At least 8 characters (Too short)
- At least one letter (Missing letter)
- At least one number (Missing number)
- At least one of these special characters: !, #, % (Missing special)
Output OK, or all related error messages (in above order). If the input string is "Hello", the output is:
Hints:
- Declare a boolean variable for each requirement.
- Use a for loop to visit each character, setting the corresponding boolean to true if satisfied (length is done differently though).
- Use the functions Character.isLetter() and Character.isDigit() to detect if a character is a letter or a number.
1 Expert Answer

Patrick B. answered 12/10/20
Math and computer tutor/teacher
import java.util.Scanner;
class PasswordValidator
{
private Scanner scanner;
public static final int PASSWORD_OK=0;
public static final int PASSWORD_TOO_SHORT=8;
public static final int PASSWORD_NO_ALPHA=4;
public static final int PASSWORD_NO_DIGIT=2;
public static final int PASSWORD_NO_SPECIAL_CHAR=1;
PasswordValidator()
{
scanner = new Scanner(System.in);
}
public int Validate(String password)
{
int iReturn = this.PASSWORD_OK;
int iLengthPW = password.length();
if (iLengthPW<8) { iReturn -= this.PASSWORD_TOO_SHORT; }
boolean alphaFlag=false;
boolean digitFlag=false;
boolean specialCharFlag=false;
for (int iLoop=0; iLoop<iLengthPW; iLoop++)
{
char chChar = password.charAt(iLoop);
Character curChChar = new Character(chChar);
if (curChChar.isLetter(chChar)) { alphaFlag=true; }
if (curChChar.isDigit(chChar)) { digitFlag = true; }
if ((chChar=='!') || (chChar=='#') || (chChar=='%')) { specialCharFlag=true; }
if (alphaFlag && digitFlag && specialCharFlag) { break; }
}
if (!alphaFlag) { iReturn -= this.PASSWORD_NO_ALPHA; }
if (!digitFlag) { iReturn -= this.PASSWORD_NO_DIGIT; }
if (!specialCharFlag) { iReturn -= this.PASSWORD_NO_SPECIAL_CHAR; }
return(iReturn);
}
public void Go()
{
System.out.print(" Please input the password :>");
String pw = scanner.next();
int iReturn = this.Validate(pw);
if (iReturn<0)
{
iReturn = -iReturn;
if ((iReturn/this.PASSWORD_TOO_SHORT)==1)
{
System.out.println(" Password Too Short: 8 characters min ");
}
iReturn %= this.PASSWORD_TOO_SHORT;
if ((iReturn/this.PASSWORD_NO_ALPHA)==1)
{
System.out.println(" Missing letter: at least one alphabetic character required ");
}
iReturn %= this.PASSWORD_NO_ALPHA;
if ((iReturn/this.PASSWORD_NO_DIGIT)==1)
{
System.out.println(" Missing number: at least one digit is required ");
}
iReturn %= this.PASSWORD_NO_DIGIT;
if (iReturn==1)
{
System.out.println(" Missing special character: at least one of ! # % is required ");
}
}
else
{
System.out.println(" Password OK ");
}
}
public static void main(String args[])
{
PasswordValidator x = new PasswordValidator();
x.Go();
}
}
Jason N.
Main.java:20: error: invalid method declaration; return type required PasswordValidator() can you help me fix?12/10/20
Jason N.
there is an invalid method decleration for PasswordValidator();12/10/20

Patrick B.
Your silly template has the class named MAIN... while I called it Passwordvalidator.... do a global search and replace to change all instances of PasswordValidator to Main12/10/20
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESOURCES section under this link12/10/20