I have the complete code provided below for a java program t
I have the complete code (provided below) for a java program that checks a password against minimum requirements but am having trouble making some minor adjustments to the code. I need the results for passwords entered (both rejected and accepted) to read the same as the provided example output. My code currently only displays one reason of rejection for every password entered when the output should display multiple reasons; and should inform the user when a password is accepted.
Output for sample passwords, \"sam\", \"sam is wise\", \"samuel\", \"samuel1\", \"samuel@home\", \"Project(123)\", \"Project#123\" and \"\".
(Provided Code)
import javax.swing.JOptionPane;
import java.util.*;
public class TestPswd
{
public static void main(String[] args)
{
//infinite loop to get password until user press cancel button
while(true)
{
//Get password
String result = JOptionPane.showInputDialog(null, \"Enter a password to check:\", \"Comment\", JOptionPane.QUESTION_MESSAGE);
//Check if user presses cancel button. If so exit
if(result==null)
//Exit from the loop
break;
//Display the result
JOptionPane.showMessageDialog(null, isValid(result));
}
}
public static boolean isValid(String password)
{
String symbol = \"@._-~#!&$\";
boolean hasLength = false;
int upperCaseCount = 0;
int lowerCaseCount = 0;
int digitCount = 0;
boolean hasSymbol = false;
if(password.length() >= 8)
{//Checks for length.
hasLength = true;
for(int i = 0; i < password.length(); i++) //For each character.
{
char c = password.charAt(i);
if(Character.isUpperCase(c) && upperCaseCount<1) //If uppercase, increment counter.
upperCaseCount++;
if(Character.isLowerCase(c) && lowerCaseCount<1) //If lowercase, increment counter.
lowerCaseCount++;
if(Character.isDigit(c) && digitCount<1) //If digit, increment counter.
digitCount++;
if(symbol.indexOf(c) != -1 && hasSymbol == false) //If has symbol, mark flag.
hasSymbol = true;
}
}
else
{
System.out.println(\"Password length should be no less than 8.\");
return false;
}
if(upperCaseCount < 1)
{
System.out.println(\"Password should contain minimum 1 uppercase letter.\");
return false;
}
if(lowerCaseCount < 1)
{
System.out.println(\"Password should contain minimum 1 lowercase letter.\");
return false;
}
if(!hasSymbol)
{
System.out.println(\"Password should contain atleast one special characters from !, @, #, $, %, ^, &, *, (, ), ?\");
return false;
}
return true;
}
}
Password \"sam\" is: Rejected. Password must have at least one uppercase character! Rejected. Missing required character Must have at least one of 0-9. Rejected. Password is too short Must be 8 or more characters! Password \"sam is wise\" is: Rejected. Invalid character(s) found Must be only A-Z, a-z, 0-9, or . Rejected. Password must have at least one uppercase character! Rejected. Missing required character Must have at least one of 0-9 . Password \"samuel1\" is: Rejected. Password must have at least one uppercase character! Rejected. Password is too short Must be 8 or more characters! Password \"samue 1@home\" is: Rejected. Password must have at least one uppercase character! Password \"Project (123) \" is: Rejected. Invalid character(s) found Must be only A-Z, a-z, 0-9, or. Password \"Project#123\" is: Accepted! Password\"\" is: Rejected. Password must have at least one uppercase character! Rejected. Missing required character Must have at least one of 0-9. Rejected. Password is too short Must be 8 or more characters! Press any key to continue .. .Solution
import javax.swing.JOptionPane;
import java.util.*;
public class TestPswd
{
public static void main(String[] args)
{
//infinite loop to get password until user press cancel button
while(true)
{
//Get password
String result = JOptionPane.showInputDialog(null, \"Enter a password to check:\", \"Comment\", JOptionPane.QUESTION_MESSAGE);
//Check if user presses cancel button. If so exit
if(result==null)
//Exit from the loop
break;
//Display the result
JOptionPane.showMessageDialog(null, isValid(result));
}
}
public static boolean isValid(String password)
{
String symbol = \"@._-~#!&$\";
boolean hasLength = false;
int upperCaseCount = 0;
int lowerCaseCount = 0;
int digitCount = 0;
boolean hasSymbol = false;
if(password.length() >= 8){//Checks for length.
hasLength = true;
for(int i = 0; i < password.length(); i++) //For each character.
{
char c = password.charAt(i);
if(Character.isUpperCase(c) && upperCaseCount<1) //If uppercase, increment counter.
upperCaseCount++;
if(Character.isLowerCase(c) && lowerCaseCount<1) //If lowercase, increment counter.
lowerCaseCount++;
if(Character.isDigit(c) && digitCount<1) //If digit, increment counter.
digitCount++;
if(symbol.indexOf(c) != -1 && hasSymbol == false) //If has symbol, mark flag.
hasSymbol = true;
}
}
if(hasLength==false){
System.out.println(\"Password length should be no less than 8.\");
}
if(upperCaseCount==0){
System.out.println(\"Password should contain minimum 1 uppercase letter.\");
}
if(lowerCaseCount==0){
System.out.println(\"Password should contain minimum 1 lowercase letter.\");
}
if(!hasSymbol && digitCount==0)
{
System.out.println(\"Password should contain atleast one of 0-9, !, @, #, $, %, ^, &, *, (, ), ?\");
}
if(hasLength==true && upperCaseCount>0 && lowerCaseCount>0 && (digitCount>0 || hasSymbol==true)){
System.out.println(\"Accepted.\");
return true;
}
else
return false;
}
}
Each condition must be a separate if condition so that every reject reason is printed. If you return false, it will not go any further down in execution. You can change the check conditions as you want now.



