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. I also need to edit the GUI output to display, \"Password is Not valid\", instead of, \"false\", when the user inputs a rejected password; and need to be able to insert a comment at the top of the title bar where it now simply reads, \"Message\".
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;
}
}
Solution
Please follow the code and comments for description :
CODE :
import javax.swing.JOptionPane;
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
if (isValid(result)) {
System.out.println(\"Accepted.!\");
JOptionPane.showMessageDialog(null, \"Accepted.!\", \"Success\", JOptionPane.INFORMATION_MESSAGE); // added the message for comments
} else {
JOptionPane.showMessageDialog(null, \"Password is Not valid.!\", \"Failure\", JOptionPane.INFORMATION_MESSAGE); // added the message for comments
}
}
}
public static boolean isValid(String password) {
String symbol = \"@._-~#!&$\";
boolean hasLength = false;
int upperCaseCount = 0;
int lowerCaseCount = 0;
int digitCount = 0;
int count = 0;
boolean hasSymbol = false;
boolean hasSpace = false;
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 (password.contains(\" \")) { // if the password has white spaces
hasSpace = true;
}
System.out.println(\"\ Password \" + \"\\\"\" + password + \"\\\"\" + \" is : \"); // prompt for the user message
if (password.length() < 8) { //Checks for length.
System.out.println(\"Rejected. Password is too Short.\ Must be 8 or more Characters.!\");
} else {
hasLength = true;
count++; // increment the count
}
if (upperCaseCount < 1) { // check for uppercase
System.out.println(\"Rejected. Password must have atleast one uppercase character.!\"); // user message
} else {
count++; // increment the count
}
if (lowerCaseCount < 1) { // check for lowecase
System.out.println(\"Rejected. Password must have atleast one lowecase character.!\");// user message
} else {
count++; // increment the count
}
if (!hasSymbol) { // check for symbols
System.out.println(\"Rejected. Missing required character.\ Must have atleast one of 0-9 . @ _ - ~ # ! & $\");// user message
} else {
count++;// increment the count
}
if (hasSpace) { // check for space
System.out.println(\"Rejected. Invalid charcater(s) found.\ Must be only A-Z a-z 0-9 . @ _ - ~ # ! & $\");// user message
} else {
count++;// increment the count
}
if (count == 5) { // check for count and return the value
return true;
} else {
return false;
}
}
}
OUTPUT :
Password \"sam\" is :
Rejected. Password is too Short.
Must be 8 or more Characters.!
Rejected. Password must have atleast one uppercase character.!
Rejected. Missing required character.
Must have atleast one of 0-9 . @ _ - ~ # ! & $
Password \"sam is wise\" is :
Rejected. Password must have atleast one uppercase character.!
Rejected. Missing required character.
Must have atleast one of 0-9 . @ _ - ~ # ! & $
Rejected. Invalid charcater(s) found.
Must be only A-Z a-z 0-9 . @ _ - ~ # ! & $
Password \"Project#123\" is :
Accepted.!
Hope this is helpful.




