I want to write a simple java program to check whether keycode matches a set of conditions.
This is what I have so far:
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();
do {
if (key1.length() < 6 || key1.length() > 8) {
System.out.println("must be at least 6 letter and max 8 letter");
return;
}
else {
boolean upper = false;
boolean lower = false;
boolean number = false;
for (char c : key1.toCharArray()) {
if (Character.isUpperCase(c)) {
upper = true;
} else if (Character.isLowerCase(c)) {
lower = true;
} else if (Character.isDigit(c)) {
number = true;
}
}
if (!upper) {
System.out.println("must contain at least one uppercase character");
return;
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
return;
} else if (!number) {
System.out.println("must contain at least one number");
return;
} else {
return;
}
}
} while (true);
System.out.println("Input keycode again");
String key2 = keycode.nextLine();
if (key1.equals(key2)) {
System.out.println("keycode matched");
} else {
System.out.println("keycode dont match");
}
The user is prompted to input a keycode.
The program first checks if it is more than 6 characters and less than 8 characters.
It then checks if it contains a lowercase, uppercase and a number.
I want it to allow user to input the password again if he were to make any mistake rather than doing all over again.
If successful, it will ask the user to input keycode again. If the two keycodes don't match,
the user is allowed to try again. After 3 unsuccessful tries, the system will reply with 'keycode mismatch'.
The part I need assistance in is allowing user to input password if it doesn't fit the requirement and password mismatch. When I enter a password less than 6 characters, I get the following output:
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
use a loop
boolean flag=false;
while(flag!=true)
{
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();
if (key1.length() < 6 || key1.length() > 8) {
System.out.println("must be at least 6 letter and max 8 letter");
}
else
{
flag=true;
}
}
Use a boolean variable say redo. And at every place you have used return, replace it with redo = true. And your while condition should be while(redo), which will run until redo is true, that it, user hasn't entered correct number.
Also, move your input reading line: -
String key1 = keycode.nextLine();
Inside your do-while loop. And also ,reset your redo variable to false in the loop, so that it doesn't remain true forever.
String[] keys = new String[2]; // Since you are reading 2 keys, so size = 2
boolean redo = false;
int count = 0; // To maintain the number of keys read. should stop at count = 2
do {
redo = false;
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode No: + " + (count + 1));
String key1 = keycode.nextLine();
if (key1.length() < 6 || key1.length() > 8) {
redo = true;
} else {
/** Your remaining code **/
if (!upper) {
System.out.println("must contain at least one uppercase character");
redo = true;
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
redo = true;
} else if (!number) {
System.out.println("must contain at least one number");
redo = true;
} else {
keys[count++] = key1; // Enter a new key in array
}
}
} while (redo || count < 2);
if (keys[0].equals(keys[1])) {
System.out.println("Keys are equal");
}
You should put the line String key1 = keycode.nextLine(); inside the do loop so that the user is queried once every time it is needed. Else, you are just stuck in a loop that will always check the same value again and again.
boolean redo = false;
do {
redo = false;
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();
if (key1.length() < 6 || key1.length() > 8) {
redo = true;
} else {
/** Your remaining code **/
if (!upper) {
System.out.println("must contain at least one uppercase character");
redo = true;
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
redo = true;
} else if (!number) {
System.out.println("must contain at least one number");
redo = true;
}
else{
System.out.println("Input keycode again");
String key2 = keycode.nextLine();
if (key1.equals(key2)) {
System.out.println("keycode matched");
} else {
System.out.println("keycode dont match");
}
}
}
} while (redo);
i manage to fit the code inside here , not sure if its the correct method to do so?
Related
The issue I am having is when someone enters a password that includes digits, uppercase, and lowercase the program still says they need all them.
I need the program to list all the requirements for a password if they are not met.
For example:
A123456789aa returns: "You need at least one lowercase. Please Enter Your Password:"
It should return: "Enter termination key"
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
boolean condition = true;
while (condition) {
System.out.print("Please Enter Your Password: ");
String password = input.nextLine();
if (password.equals("endofinput")) {
System.out.print("Your password is valid"); condition = false;
} else {
System.out.print(passCheck(password));
condition = true;
}
}
input.close();
}
public static String passCheck(String password) {
String specialChars = "/*!##$%^&*()\"{}_[]|\\?/<>,." + " ";
if (password.length() < 8) {
return ("You need at least 8 characters. ");
} for (int i = 0; i < password.length(); i++) {
if (specialChars.contains(password.substring(i)))
return ("Your password cannot contain special characters. ");
else if (password.equals("password"))
return ("Your password cannot be password. ");
else if(!Character.isUpperCase(password.charAt(i)))
return ("You need at least one uppercase. ");
else if (!Character.isLowerCase(password.charAt(i)))
return ("You need at least one lowercase. ");
else if (!Character.isDigit(password.charAt(i)))
return ("You need at least one digit. ");
}
return "Enter termination key";
}
your passCheck method iterates over all chars, and returns a result if one char does not fullfill one of your requirements. An alternative would be to assume that the input is not valid, until all requirements are fullfilled:
boolean foundUppercase = false;
boolean foundLowercase = false;
boolean foundDigits = false;
//Dont have to do the check for password as input for every letter in the word.
if (password.equals("password"))
return ("Your password cannot be password. ");
for (int i = 0; i < password.length(); i++) {
if (specialChars.contains(password.substring(i)))
//only in this case we can for sure say that the input is not valid
return ("Your password cannot contain special characters. ");
else if (Character.isUpperCase(password.charAt(i)))
foundUppercase=true;
else if (Character.isLowerCase(password.charAt(i)))
foundLowercase= true;
else if (Character.isDigit(password.charAt(i)))
foundDigits = true;
}
if (!foundUppercase) {
// uppercase letter missing
return ("You need at least one uppercase. ");
} else if (!foundLowercase) {
// lower case letter missing
return ("You need at least one lowercase. ");
} else if (!foundDigits) {
// missing digits
return ("You need at least one digit. ");
}
return "Enter termination key";
You can use this validation function
public static String passCheck(String password) {
//String specialChars = "/*!##$%^&*()\"{}_[]|\\?/<>,." + " ";
String expected_pattern = "^[a-zA-Z0-9]{8,}$";
String lowercase_pattern = "(.*)[a-z]+(.*)";
String uppercase_pattern = "(.*)[A-Z]+(.*)";
String digit_pattern = "(.*)[0-9]+(.*)";
if (password == null || password.length() < 8) return ("You need at least 8 characters. ");
if (password.toLowerCase().equals("password")) return ("Your password cannot be password. ");
if (!password.matches(lowercase_pattern)) return ("You need at least one lowercase. ");
if (!password.matches(uppercase_pattern)) return ("You need at least one uppercase. ");
if (!password.matches(digit_pattern)) return ("You need at least one digit. ");
if (!password.matches(expected_pattern)) return ("Your password cannot contain special characters. ");
return "Enter termination key";
}
I want to write a program that checks the inserted password for:
Length is minimum of 8
At least 1 uppercase letter
At least 1 lowercase letter
At least 3 digits
I wrote this program, but it doesn't give me the right output:
import java.util.Scanner;
public class Question5 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Please enter your password: ");
String input = in.nextLine();
boolean flag = validate(input);
if (flag = true) {
System.out.println("password verified");
}
else {
System.out.println("not a good password");
}
}
public static boolean validate(String input) {
boolean flag = false;
int uppercaseCounter = 0;
int lowercaseCounter = 0;
int digitCounter = 0;
int letterCounter = 0;
for (int i = 0; i<(input.length()); i++) {
int totalCounter = digitCounter + letterCounter;
if (totalCounter >= 8 && digitCounter >= 3 && uppercaseCounter > 0 && lowercaseCounter > 0) {
flag = true;
}
else {
if (Character.isDigit(i)) {
digitCounter++;
}
if (Character.isLetter(i)) {
letterCounter++;
}
if (Character.isUpperCase(i)) {
uppercaseCounter++;
}
if (Character.isLowerCase(i)) {
lowercaseCounter++;
}
}
}
return flag;
}
}
Can someone help me with this? Thank you very much!
Here is the catch:
if (flag = true)
{
System.out.println("password verified");
}
= is an assignment operator == is the relational operator. To fix, do flag==true.
Also, in your method, you are comparing i, which is the counter, and not the
char At i. So do this
if(Character.isDigit(input.charAt(i))){ //Do this for all Character.isSomething() Methods
for all the checks you make.
You are actually checking the i counter in your various if instead of the input string...
use something like
char c = s.charAt(i);
and check the input chars
moreover you should change the check if(flag = true) with if(flag)
Change
if (flag = true)
{
System.out.println("password verified");
}
else
{
System.out.println("not a good password");
}
to
if (flag)
{
System.out.println("password verified");
}
else
{
System.out.println("not a good password");
}
When you write if(flag=true) then you are doing an assignment operation and not an equality comparison.
Also, the logic should be Character.isDigit(input.charAt(i)) since you want to check the character at i and not i itself.
To conclude, I would like to say that this problem would be fun to solve with Regular Expressions. Check this tutorial on regular expressions in Java.
In addition to other answers, this code should be moved below the counter increments:
int totalCounter = digitCounter + letterCounter;
if (totalCounter >= 8 && digitCounter >= 3 && uppercaseCounter > 0 && lowercaseCounter > 0) {
flag = true;
}
Otherwise, you run the risk of returning false when your password would become valid on the last character.
I'm writing a password validation program and having some issues with the or statement that checks for special characters. I'm not sure what is wrong with it, the rest of the program seems to function properly, but entering a password that matches the criteria, for example, P#ndas123$%, just results in an infinite loop.
I believe that the problem is related to my big statement for the !pass.contains("") portion. If that is right, could someone help me understand why the statement is incorrect?
import java.util.Scanner;
public class DylanJacksonProg5 {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
boolean valid;
valid = true;
do {
//Password rules declared to user
System.out.println("Password Verifier");
System.out.println("");
System.out.println("Enter a password that meets the following rules: ");
System.out.println(" Is at least 8 characters long");
System.out.println(" Contains at least 1 lower letter character");
System.out.println(" Contains at least 1 upper letter character");
System.out.println(" Contains at least 1 numeric digit");
System.out.println(" Contains at least 1 special character of the following set: !##$%^&*");
System.out.println(" Does not contain the word 'and' or the word 'the'");
//Gathering password from user
System.out.println("Enter your password: ");
String pass;
pass = user_input.next();
//Validating password to rules
if (pass.length() < 8) {
int shortlen;
shortlen = 8 - pass.length();
System.out.println("Password is too short. Please add " + shortlen + " characters");
valid = false;
} else
System.out.println("Password length is acceptable.");
//Check for character cases
boolean hasUppercase = !pass.equals(pass.toLowerCase());
boolean hasLowercase = !pass.equals(pass.toUpperCase());
if (!hasUppercase) {
System.out.println("Must have an uppercase Character");
valid = false;
}
if (!hasLowercase) {
System.out.println("Must have a lowercase Character");
valid = false;
}
if (hasUppercase) {
System.out.println("Has an upper case character");
}
if (hasLowercase) {
System.out.println("Has a lowercase Character");
}
//Check for digit
for (int i = 0; i < pass.length(); i++) {
char x = pass.charAt(i);
if (!Character.isDigit(x)) {
valid = false;
}
}
//check for special character
if (!pass.contains("!") || !pass.contains("#") || !pass.contains("#") || !pass.contains("$") ||
!pass.contains("%") || !pass.contains("^") || !pass.contains("&") || !pass.contains("*"))
{
valid = false;
System.out.println("Password requires a special character from the following: !##$%^&*");
}
// check for 'and'
if (pass.contains("and")) {
valid = false;
System.out.println("Cannot contain 'and'");
}
//check for 'the'
if (pass.contains("the")) {
valid = false;
System.out.println("Cannot contain 'the'");
}
System.out.println("");
}
while (!valid);
}
}
I'll tell you one problem straight up front.
Since you set valid to true before the validation loop, and only ever set it to false within the loop, entering an invalid password as your first attempt will result in the loop never exiting.
The setting of valid to true should be the first thing done within the loop.
On top of that, your checks each individually apply to every character in the input string, a situation which is impossible. For example, you check whether the entire string is equal to its lowercased variant, then you check whether it's also equal to its uppercased variant.
That means any string with a letter in it will be deemed invalid since xyZZy cannot be equal to both xyzzy and XYZZY at the same time.
It would be more correct to do something like (pseudo-code, and cut down to a few conditions just to show the method):
do:
hasLowercase = false
hasUpperCase = false
hasNumeric = false
isLongEnough = false
get string from user
if len(string) >= 8:
isLongEnough = true
for each character in string:
if character is lowercase:
hasLowerCase = true
if character is uppercase:
hasUpperCase = true
if character is numeric:
hasNumeric = true
isValid = hasLowerCase && hasUpperCase && hasNumeric && isLongEnough
until isValid
This checks each individual character so that any of them (rather than requiring that all of them) will mark a condition as true.
The OR operators will conflict if you don't have all of the special characters in the password, because in your condition statements, if you have "!" but not "#", it will still result in a false value.
What you can do is check for each special character separately.
Sorry for the lack of explanation, but it is your negation statements in your special character checking block. Usually using !condition works, but it didn't on this online IDE I am using. Not really sure why.
This code will work:
//check for special character
if (pass.contains("!") || pass.contains("#") || pass.contains("#") || pass.contains("$") ||
pass.contains("%") || pass.contains("^") || pass.contains("&") || pass.contains("*"))
{
valid = true;
System.out.println("Password requires a special character from the following: !##$%^&*");
}
else
{
valid = false;
}
There are multiple problems with your code, which are unrelated to each other:
You never set valid to true in the loop, which means that if you enter 1 wrong password, valid will be and always stay false, which means that no matter how many correct passwords you enter afterwards, the loop will repeat infinitely. In other words: the program will only stop if you enter a correct password at the very first attempt.
The big boolean expression is not checking whether the password contains any of the characters, it is checking whether the password contains all characters. Draw the truth table or apply DeMorgan's Laws, and you will see:
!p.c("!") || !p.c("#") || !p.c("#") || !p.c("$") || !p.c("%") || !p.c("^") || !p.c("&") || !p.c("*")
// is the same per DeMorgan's Laws as
!(p.c("!") && p.c("#") && p.c("#") && p.c("$") && p.c("%") && p.c("^") && p.c("&") && p.c("*"))
As soon as you find a single character that is not a digit, you reject the password, IOW all characters must be digits.
It should be obvious that #2 and #3 contradict each other, so it it simply impossible to enter a valid password, so even if you fix #1, it will still not be possible to exit the loop.
There are a 3 issues with your code:
valid must be set to true inside your loop before any checks are done on input:
do {
valid = true;
...
lower & upper case checks should be done differently, for example:
boolean hasUppercase = false;
boolean hasLowercase = false;
for (int i = 0; i < pass.length(); i++){
if (Character.isUpperCase(pass.charAt(i))){
hasUppercase = true;
}
if (Character.isLowerCase(pass.charAt(i))){
hasLowercase = true;
}
}
your checks for special character are incorrect, you could use below instead:
if (!(pass.contains("!") || pass.contains("#") || pass.contains("#") || pass.contains("$") || pass.contains("%") || pass.contains("^") || pass.contains("&") || pass.contains("*"))){
valid = false;
}
EDIT: actually there's another one:
4. Your check for digit also won't work, you could use below instead:
boolean isDigit = false;
for (int i = 0; i < pass.length(); i++) {
char x = pass.charAt(i);
if (Character.isDigit(x)) {
//valid = false;
isDigit = true;
break;
}
}
if (!isDigit)
valid = false;
Here's how OR operator works:
if( condition1 || condition2 || condition3 || condition4)
{
// .....your code;
}
If condition1 is true,then remaining conditions will not be checked and program will move to inside your IF block.However,if Condition1 is false,then condition2 will be checked.If this is true,then remaining conditions will not be checked,otherwise(if it is false) condition3 will be checked.and so on.However as other peoples have already mentioned there is more problem with your code than just with OR operator.
For example,for password=P#ndas123$%,it does not contain "!",so !pass.contain("!") will give true and valid will be set as false thus causing infinite loop.
Im a rookie Java coder, and I am trying to make a very basic username/password program. The username part of it is working fine, but when I get to the password it gives me some weird problems. I figured out that for example when it checks for an Uppercase letter, if it finds one its all good, but if it doesn't, it prints the error message for every single character in the password. It does this with the number check and the length check as well. If any of you guys could explain this to me rather simply since I am still new to java, that would be awesome. Thanks!
do
{
if (count3 >0)
{
System.err.println("- At least 1 Uppercase");
System.err.println("- At least 1 number");
System.err.println("- At least 7 characters long.");
}
regPassword = input.nextLine();
regPasswordLen = regPassword.length();
for(int count = 0; count < regPasswordLen; count++)
{
if(Character.isUpperCase(regPassword.charAt(count)))
regPasswordUppercaseCheck = true;
else
{
System.err.println("Your password did not contain an Uppercase letter");
regPasswordUppercaseCheck = false;
}
if(regPassword.contains("1") || regPassword.contains("2") ||
regPassword.contains("3") || regPassword.contains("4") ||
regPassword.contains("5") || regPassword.contains("6") ||
regPassword.contains("7") || regPassword.contains("8") ||
regPassword.contains("9") || regPassword.contains("0"))
regPasswordNumCheck = true;
else
{
System.err.println("Your password did not contain at least 1 number.");
regPasswordNumCheck = false;
}
if (regPasswordLen >=7)
regPasswordLengthCheck = true;
else
{
System.err.println("Your password did not meet the minimum length requirements.");
regPasswordLengthCheck = false;
}
}
count3++;
}
while(!regPasswordUppercaseCheck || !regPasswordNumCheck || !regPasswordLengthCheck);
System.out.println("test");
You used same variable every time for "if and else" for every different char i.e. regPasswordUppercaseCheck, if every char of your input is in uppercase except the last char, the variable will contain false.
I think you use count3 for making sure that inner code will run single time but if while goes false and count3 condition is remain true then code will stuck in a infinite loop.
Use
while(regPasswordUppercaseCheck && regPasswordNumCheck && regPasswordLengthCheck); for simplicity.
A few things you can change in your program.
do
{
if (count3 >0)
{
System.err.println("- At least 1 Uppercase");
System.err.println("- At least 1 number");
System.err.println("- At least 7 characters long, but no more than 15 characters.");
}
regPassword = input.nextLine();
regPasswordLen = regPassword.length();
// this check only needs to happen once per password, no need to check it in the for loop. You also specified that the length should not exceed 15 characters, so I threw that in as well
if (regPasswordLen < 7 || regPasswordLen > 15)
System.err.println("Your password did not meet the length requirements.");
// by default, we set these flags to false, and only make them true if the requirements are satisfied
regPasswordUppercaseCheck = false;
regPasswordNumCheck = false;
for(int count = 0; count < regPasswordLen; count++)
{
// store the value of regPassword.charAt(count) in a local variable for reusability
char current = regPassword.charAt(count);
if(Character.isUpperCase(current))
regPasswordUppercaseCheck = true;
// checks if the character is a digit or not
if(current >= '0' && current <= '9')
regPasswordNumCheck = true;
}
if (!regPasswordNumCheck)
System.err.println("Your password did not contain at least 1 number.");
if (!regPasswordUppercaseCheck)
System.err.println("Your password did not contain an Uppercase letter");
count3++;
}
while(!regPasswordUppercaseCheck || !regPasswordNumCheck || !regPasswordLengthCheck);
your checking for uppercase is not done right because the loop
for(int count=0;count<regPasswordLength;count++) should not contain the checking if the password contains a number nor the checking if the password is longer than 7 characters so the loop should look like this
for (int count = 0; count < regPasswordLen; count++) {
if (Character.isUpperCase(regPassword.charAt(count)))
{regPasswordUppercaseCheck = true;break;}
}
i use break here to get out of the loop the moment i found that password contains an uppercase after some modifications your code can look like this
do {
if (count3 > 0) {
System.err.println("- At least 1 Uppercase");
System.err.println("- At least 1 number");
System.err
.println("- At least 7 characters long, but no more than 15 characters.");
}
regPassword = input.nextLine();
regPasswordLen = regPassword.length();
for (int count = 0; count < regPasswordLen; count++) {
if (Character.isUpperCase(regPassword.charAt(count)))
{regPasswordUppercaseCheck = true;break;}
}
if(regPasswordUppercaseCheck==false){
System.err
.println("Your password did not contain an Uppercase letter");
regPasswordUppercaseCheck = false;
}
regPasswordNumCheck = regPassword.contains("1") || regPassword.contains("2")
|| regPassword.contains("3")
|| regPassword.contains("4")
|| regPassword.contains("5")
|| regPassword.contains("6")
|| regPassword.contains("7")
|| regPassword.contains("8")
|| regPassword.contains("9")
|| regPassword.contains("0");
if(regPasswordNumCheck==false) {
System.err
.println("Your password did not contain at least 1 number.");
regPasswordNumCheck = false;
}
if (regPasswordLen >= 7)
regPasswordLengthCheck = true;
else {
System.err
.println("Your password did not meet the minimum length requirements.");
regPasswordLengthCheck = false;
}
count3++;
} while (!regPasswordUppercaseCheck || !regPasswordNumCheck
|| !regPasswordLengthCheck);
System.out.println("test");
I am trying to complete my programming assignment, I have to create a "Password verifier" that has 1 uppercase, 1 lowercase, 1 number, and 1 special character. If invalid, it must show "invalid" plus the rule that it has shown as false. The problem I have, is not an error message, but I always get a positive response, plus all the violated rules, plus it reads them twice. Little help here?
import java.util.Scanner;
public class PasswordVerifier {
public static void main(String[] args) {
System.out.println("Password Verifier");
System.out.println("Enter a password that meets the following rules: ");
System.out.println("-Must be at least 8 characters long" + '\n' +
"-Must contain at least 1 lower case character" + '\n' +
"-Must contain at least 1 upper case character" + '\n' +
"-Must contain at least 1 numeric digit" + '\n' +
"-Must contain at least 1 special character from the set: !##$%^&*" + '\n' +
"-Must not contain the word 'and' or the word 'end'");
String password;
String contains1 = "and";
String contains2 = "end";
String special = "!##$%^&*";
Scanner stdIn = new Scanner(System.in);
boolean digit = false; //Has at least 1 digit
boolean upper = true; //Has at least 1 upper case letter
boolean lower = true; //Has at least 1 lower case letter
boolean hasspecial = true; //Has at least 1 special character
boolean length = true; //Has at least 8 digits
boolean endand = true; //Does not contain end or and
boolean valid = false; //Is the password valid?
System.out.println("Enter password: ");
password = stdIn.nextLine();
int result;
result = password.indexOf(contains1);
if (result == -1) {
System.out.println("");
} else {
System.out.println("Must not contain the word 'and'");
}
int result2;
result2 = password.indexOf(contains2);
if (result2 == -1) {
System.out.println("");
} else {
System.out.println("Must not contain the word 'end'");
}
if (password.length() < 8) {
System.out.println("Must be at least 8 characters long.");
} else {
System.out.print("");
}
for (int i = 0; i < password.length(); i++) {
if (!(Character.isUpperCase(password.charAt(i)))) ;
{
upper = false;
valid = false;
i++;
}
if (!(Character.isLowerCase(password.charAt(i)))) ;
{
lower = false;
valid = false;
i++;
}
if (!(Character.isDigit(password.charAt(i)))) ;
{
digit = false;
valid = false;
i++;
}
if (!(password.matches(special))) ;
{
hasspecial = false;
valid = false;
}
if (upper != true) {
System.out.println("Must contain an upper case letter.");
}
if (lower != true) {
System.out.println("Must contain a lower case letter.");
}
if (digit != true) {
System.out.println("Must contain a numeric digit.");
}
if (hasspecial != true) {
System.out.println("Must contain a special character.");
}
if (valid) {
System.out.println("Valid.");
} else if (valid != true) {
System.out.println("Invalid.");
}
}
}
}
One thing. All your if lines are wrong.
You are adding a ';' after the if. That means, if the if condition is met, do nothing, then always execute the following lines.
Another thing, when you do
if (!(password.matches(special))) {
hasspecial = false;
valid = false;
}
You are asking for the password to match the string "!##$%^&(asterisk)", which it probably doesn't. So, since you are assuming that the string contains a special character, hasspecial will always be true. You want to write something like password.matches(".(asterisk)[!##$%^&*].(asterisk)) (Probably escaping some characters).
Finally, you are saying if a letter doesn't meet a condition, set the marker to false. So, if the following letter does meet the condition, the marker will still be false. You should usually approach this kind of problems by assuming that all of the conditions are false and then setting the markers to true once the condition is met.
Note: I wrote (asterisk) instead of *, becasue the text gets highlighted and I don't know how to escape it :(