I'm a student of CIS taking a Java class (hoping to become a programmer). Anyhow, I have an assignment to create a program that checks a password for validity. If the password is not valid, it is to print a message. If the password is valid, the user is to reinsert the password. If they both match, it is to print a message accepting. If they do not match, it is to print a message. The password requirements are:
- Must be at least 8 characters in length
- Must contain one number
- Must contain one letter
- Must contain one special character
- Cannot have 3 or more identical characters
- Cannot contain spaces
- Cannot start with ! or ?
I am using netbeans.
package pass1;
import java.util.Scanner;
public class Pass1 {
//Main method asks for pasword from user and calls upon methods to verify password requirements and password 1 and 2 match
public static void main(String[] args) {
//declare variables
String pass1;
String pass2;
boolean passvalid;
boolean passmatch;
//initialize new instance of scanner
Scanner kb = new Scanner(System.in);
//get password
System.out.println("Please input password. (Must be at least 8 characters, contain a number, letter, and special character");
pass1 = kb.nextLine();
//initialize instance of passisvalid to check password for requirements
passvalid = passisvalid(pass1);
//if pass is valid, allow user to reinsert password
if (passvalid){
System.out.println("Please reinsert password:");
pass2 = kb.nextLine();
//initialize instance of passismatch to check passwords match
passmatch = passismatch(pass1, pass2);
//if passwords do not match, print message
if (!passmatch)
{
System.out.println("Passwords do not match.");
}
//if passwords match, print message
else if (passmatch)
{
System.out.println("Password set.");
}
}
//if password is not valid, print message
else
{
System.out.println("Password is not valid:");
}
}
/*************************************************************************************/
//this method check that user inputted password meets requirements, and returns boolean value
public static boolean passisvalid(String password) {
//declare variables
boolean letter;
boolean digit;
boolean space;
boolean length;
boolean start1;
boolean start2;
boolean valid;
boolean special;
//initialize variables
valid=false;
letter=false;
digit=false;
space=false;
special=false;
length = false;
start1=false;
start2=false;
//initialize count
for(int i=0;i<password.length();i++)
{
char s=password.charAt(i);
//check for letter in password
if(Character.isLetter(s))
{
letter = true;
}
//check for number in password
if(Character.isDigit(s))
{
digit = true;
}
//check for spaces in password
if(Character.isSpaceChar(s))
{
space=true;
}
//check for special characters in password
if(!Character.isDigit(s) && !Character.isLetter(s))
{
special=true;
}
}
//check password length
if (password.length() > 8)
{
length=true;
}
//check password start with ? or !
else if (password.startsWith("?"))
{
start1=true;
}
else if (password.startsWith("!"))
{
start1=true;
}
//requirements of password for boolean true
if(letter && digit && special && length)
{
valid = true;
}
//return boolean false if detect start with ? or !, or spaces in password
if(start1||start2||space)
{
valid = false;
}
return valid;
}
/**********************************************************************************/
//this method checks that both user entered passwords match
public static boolean passismatch(String password1, String password2){
//declare variables
boolean match;
//initialize variables
match=false;
//compare password strings
if (password1.equals(password2))
{
match= true;
}
return match;
}
}
So I've been able to figure all except for making sure there are no consecutive repeating characters. I really don't even know where to begin with this. I've searched for hours on a few forums to try and find a solution and put it all together but without seeing an example or just something I can't figure it out for the life of me. Any help would be IMMENSELY appreciated.