Please help me with this password validation - While Loop Running Once
-
I am tryint to validate a password that has the following rules: can't be the same as username, at least 8 characters, and must have a uppercase letter and special character. Any help you can offer would be AMAZING! :)
import java.util.Scanner;
public class Main {
static String userName = "johndoe";
static String password = "ABC_1234";
static boolean validPassword = false;public static void main(String\[\] args) { System.out.println("Please Change Your Password"); System.out.println(""" Please make sure it is 8 characters long, Contains an uppercase letter, Contains a special character, Does not contain your username, And is not the same as the previous password """); checkPassword(); } public static void checkPassword() { System.out.println(); System.out.print("What is your new password? "); Scanner scanner = new Scanner(System.in); String newPassword = scanner.next(); //System.out.println(newPassword); while (!validPassword) { if (newPassword.equalsIgnoreCase(userName)) { System.out.println("Your password can't be the same as your username"); System.out.print("What is your new password? "); newPassword = scanner.next(); } if (newPassword.length() < 8) { System.out.println("You password must be at least 8 characters"); System.out.print("What is your new password? "); newPassword = scanner.next(); //validPassword = true; } for (int i = 0; i < newPassword.length(); i++) { char ch = newPassword.charAt(i); if (!Character.isUpperCase(ch) || (Character.isDigit(ch) && Character.isLetter(ch) && Character.isWhitespace(ch))) { System.out.println("Please make sure your password has an uppercase and/or special character"); newPassword = scanner.next(); validPassword = true; } else { System.out.println("stop"); validPassword = false; } } } }
}
-
I am tryint to validate a password that has the following rules: can't be the same as username, at least 8 characters, and must have a uppercase letter and special character. Any help you can offer would be AMAZING! :)
import java.util.Scanner;
public class Main {
static String userName = "johndoe";
static String password = "ABC_1234";
static boolean validPassword = false;public static void main(String\[\] args) { System.out.println("Please Change Your Password"); System.out.println(""" Please make sure it is 8 characters long, Contains an uppercase letter, Contains a special character, Does not contain your username, And is not the same as the previous password """); checkPassword(); } public static void checkPassword() { System.out.println(); System.out.print("What is your new password? "); Scanner scanner = new Scanner(System.in); String newPassword = scanner.next(); //System.out.println(newPassword); while (!validPassword) { if (newPassword.equalsIgnoreCase(userName)) { System.out.println("Your password can't be the same as your username"); System.out.print("What is your new password? "); newPassword = scanner.next(); } if (newPassword.length() < 8) { System.out.println("You password must be at least 8 characters"); System.out.print("What is your new password? "); newPassword = scanner.next(); //validPassword = true; } for (int i = 0; i < newPassword.length(); i++) { char ch = newPassword.charAt(i); if (!Character.isUpperCase(ch) || (Character.isDigit(ch) && Character.isLetter(ch) && Character.isWhitespace(ch))) { System.out.println("Please make sure your password has an uppercase and/or special character"); newPassword = scanner.next(); validPassword = true; } else { System.out.println("stop"); validPassword = false; } } } }
}
if (!Character.isUpperCase(ch) || (Character.isDigit(ch) && Character.isLetter(ch) && Character.isWhitespace(ch))) {
This if clause (and the following statements) does not make much sense as you are testing for multiple conditions existing at the same time. You need to test each character in turn and keep a count of the valid types (upper case, digit, special). Then when all characters have been tested check that each count is greater than zero to signify a valid password.
-
I am tryint to validate a password that has the following rules: can't be the same as username, at least 8 characters, and must have a uppercase letter and special character. Any help you can offer would be AMAZING! :)
import java.util.Scanner;
public class Main {
static String userName = "johndoe";
static String password = "ABC_1234";
static boolean validPassword = false;public static void main(String\[\] args) { System.out.println("Please Change Your Password"); System.out.println(""" Please make sure it is 8 characters long, Contains an uppercase letter, Contains a special character, Does not contain your username, And is not the same as the previous password """); checkPassword(); } public static void checkPassword() { System.out.println(); System.out.print("What is your new password? "); Scanner scanner = new Scanner(System.in); String newPassword = scanner.next(); //System.out.println(newPassword); while (!validPassword) { if (newPassword.equalsIgnoreCase(userName)) { System.out.println("Your password can't be the same as your username"); System.out.print("What is your new password? "); newPassword = scanner.next(); } if (newPassword.length() < 8) { System.out.println("You password must be at least 8 characters"); System.out.print("What is your new password? "); newPassword = scanner.next(); //validPassword = true; } for (int i = 0; i < newPassword.length(); i++) { char ch = newPassword.charAt(i); if (!Character.isUpperCase(ch) || (Character.isDigit(ch) && Character.isLetter(ch) && Character.isWhitespace(ch))) { System.out.println("Please make sure your password has an uppercase and/or special character"); newPassword = scanner.next(); validPassword = true; } else { System.out.println("stop"); validPassword = false; } } } }
}
You're not handling your "validPassword" flag in all cases (setting / resetting). And the while loop should be in Main ... calling the check routine (which returns a result) and reading the next try if necessary.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
if (!Character.isUpperCase(ch) || (Character.isDigit(ch) && Character.isLetter(ch) && Character.isWhitespace(ch))) {
This if clause (and the following statements) does not make much sense as you are testing for multiple conditions existing at the same time. You need to test each character in turn and keep a count of the valid types (upper case, digit, special). Then when all characters have been tested check that each count is greater than zero to signify a valid password.
Ok... I will try that, I thought you could check for multiple conditions in that way as I saw it done that way somewhere.
-
You're not handling your "validPassword" flag in all cases (setting / resetting). And the while loop should be in Main ... calling the check routine (which returns a result) and reading the next try if necessary.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
Gotcha. So based on what you are describing, this is the reason the loop is running only once and not continuing until the valid result is met? I will try that. Thank you.
-
Ok... I will try that, I thought you could check for multiple conditions in that way as I saw it done that way somewhere.
No, it could never be done that way; look at the test:
if (!Character.isUpperCase(ch) // if the character is NOT upper case
|| // OR
(Character.isDigit(ch) // it is a digit
&& // AND
Character.isLetter(ch) // it is a letter
&& // AND
Character.isWhitespace(ch)) // it is a whitespace - it cannot be all three of those last testsSo when this test fails (as it mostly will) you request a new password, but then set
validPassword = true;
, without checking anything else. -
Gotcha. So based on what you are describing, this is the reason the loop is running only once and not continuing until the valid result is met? I will try that. Thank you.
Thanks for sharing this great information. PTE Coaching Classes in Chandigarh
-
Ok... I will try that, I thought you could check for multiple conditions in that way as I saw it done that way somewhere.
If your password validation using a while loop is running only once, it could be due to incorrect loop conditions or improper updating of variables within the loop. Ensure that the condition is set to continue until the desired criteria are met, and that the loop correctly prompts for input after each failed attempt. Double-check that any flags or counters are correctly initialized and updated within the loop. Regarding Smartplayapks, proper password validation is crucial for securing
downloads and user data, ensuring that the platform maintains a high level of security for its users.