Reputation: 45
This one will be quick, I have to make a program that checks to see if a password is valid or not. It has to be atleast 8 characters, have one lower, one upper case char, and atleast one special char to be valid. Otherwise, the password will be invalid. Everything seems to be alright, however it doesn't seem to ever checkout as valid even when it is. I feel like it has to do with the character location or something but I cannot pin what it is exactly. EDIT: Updated to include Regex The code is as follows:
/* Class: CS1301
* Section: 9:30
* Term: Fall 2015
* Name: Matthew Woolridge
* Instructor: Mr. Robert Thorsen
* Assignment: Assignment 6
* Program: 3
* ProgramName: PasswordTest
* Purpose: The program prompts the user to input a password and says if it is valid or invalid
* Operation: The information is statically instantiated in the code and
* the data is output to the screen.
* Input(s): The input the password
* Output(s): The output will be valid or invalid
* Methodology: The program will use loops to determine if valid or invalid
*
*/
import java.lang.*;
import java.util.*;
public class PasswordTest
{
public static void main (String[] args)
{
/******************************************************************************
* Declarations Section *
******************************************************************************/
/****************************CONSTANTS********************************/
String pass;
int i;
boolean valid=false;
Scanner scan = new Scanner(System.in); //Initializes the scanner
//"^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$" //RegexChecker
/******************************************************************************
* Inputs Section *
******************************************************************************/
System.out.print("Please input a password: ");
pass = scan.nextLine();
/****************************variables********************************/
//*********************Using while loop so in processing*********************//
/******************************************************************************
* Processing Section *
******************************************************************************/
for (i = 0; i<pass.length(); i++)
{
if(pass.matches("^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$")){
valid=true;
}
}
if (valid==true){
System.out.print("Entered Password: " + pass);
System.out.print("\nThe pass is: Valid!");
}
else{
System.out.print("Entered Password: " + pass);
System.out.print("\nThe pass is: Invalid!");
}
/******************************************************************************
* Outputs Section *
******************************************************************************/
//*********************The outputs are in the processing*****************//
} //Ends string
} //Ends program
Upvotes: 0
Views: 213
Reputation: 1966
Using regex would solve your problem
Your parameter to be a valid password are
This regex would be just fine
^(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$
Example
public class RegexDemo {
public static void main(String [] args)
{
String text1 ="ghhthyuj";
String text2 ="G$hthyu5";
System.out.println(text1.matches("^(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$"));
System.out.println(text2.matches("^(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*/&%^*$#@!~+_]).+$"));
}
}
Output : text1 = false text2 = true
A short explanation:
^ // the start of the string
(?=.*[a-z]) // use positive look ahead to see if at least one lower case letter exists
(?=.*[A-Z]) // use positive look ahead to see if at least one upper case letter exists
(?=.*[0-9]) // use positive look ahead to see if at least one digit exists
(?=.{8,}) // use positive look ahead to see if length of string is at least 8 charachters
(?=.*[*/&%^*$#@!~+_]) // use positive look ahead to see if at least one special character exists
.+ // gobble up the entire string
$ // the end of the string
Upvotes: 2
Reputation: 28126
You have a loop in your Processing Section, that makes only one iteration, before break happens in the else block of the last if-else statement. Try to move it out of a loop, to check the whole password, not only the first letter of the password.
And one more, you don't need to check the length of the password every time in a loop, you can do it only once.
for (i = 0; i<pass.length(); i++)
{
verify = pass.charAt(i);
if (pass.contains(special))
{
specialCheck=true;
}
if (Character.isUpperCase(verify)){
upCheck = true;
}
if (Character.isLowerCase(verify)){
lowCheck=true;
}
if (Character.isDigit(verify)){
digitCheck = true;
}
if(upCheck==true && lowCheck==true && digitCheck==true){
valid = true;
break;
}
}
if(valid == true && pass.length()>=8) {
System.out.print("Entered Password: " + pass);
System.out.print("\nThe pass is: Valid!");
} else {
System.out.print("Entered Password: " + pass);
System.out.print("\nThe pass is: Invalid!");
}
Upvotes: 2