Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I imported the Scanner class so that I can accept userInput
- import java.util.Scanner;
- public class Test {
- public static void main(String[] args) {
- //****INITIALIZING VARIABLES****//
- /* Variables are initialized up here so that they can easily be found and changed for tests.
- * I initialized the Scanner and assigned it to a variable name "userInput" to make it usable in the
- * rest of the program. */
- Scanner userInput = new Scanner(System.in);
- /* These checks will be referred upon to check how many times the password contains an upper case
- * letter(upCheck), a lower case letter(loCheck), a number(nrCheck) and a symbol(syCheck).*/
- int upCheck, loCheck, nrCheck, syCheck;
- /* These checks will be referred upon when checking how many times the appropriate character is
- * followed by a different character making the password stronger.*/
- int upCheck2, loCheck2, nrCheck2, syCheck2;
- //****OBTAINING INFORMATION FROM USER****//
- /* I prompt the user to enter his/her email address which will be stored in the variable "email".
- * This is so that I can check whether or not the password contains part of the email address. */
- System.out.printf("%-51s", "Enter your email address");
- String email = userInput.nextLine();
- /* I grab the index of the @ sign in the email which is used as the last parameter in obtaining
- * the user name of the user through the substring method.*/
- int atSign = email.indexOf('@');
- /* If there is no @ symbol in the email, the variable atSign will return -1. This means that
- * obtaining the user name of the email will be impossible. I fix this by figuring out of
- * atSign is -1 and if it is, it will continually prompt the user until they enter a valid
- * email address containing the @ symbol.*/
- while (atSign == -1) {
- System.out.printf("%-51s", "Please enter a valid email address");
- email = userInput.nextLine();
- atSign = email.indexOf('@');
- }
- String username = email.substring(0, atSign);
- /* I then prompt the user to enter his/her year of birth. Later on in the program, I will be testing
- * to see if the password contains the last two digits of the user's year of birth. */
- System.out.printf("%-51s", "Enter your year of birth");
- String birth = userInput.nextLine();
- /* I check to see if the birth year's length is 4. If it is not, it is not a valid birth year. I then
- * continue to prompt the user for a proper birth year.*/
- while (birth.length() != 4) {
- System.out.printf("%-51s", "Please enter a valid birth year");
- birth = userInput.nextLine();
- }
- /* The next variable pulls the last two numbers from the user's birth year. The last two numbers are
- * needed to check if they are in the password, which would make the password Invalid.class */
- String birth2 = birth.substring(2, 4);
- /* The last prompt asks the user for their password. Without this piece of information, this program
- * would not exist */
- System.out.printf("%-51s", "Enter your password");
- String password = userInput.nextLine();
- //****PASSWORD VERIFICATION****//
- // Sets the password check to true to make sure that the following while loop actually runs
- boolean pCheck = true;
- /* The "mainloop:" is used as a check point that the program can refer back to. There is many times
- * when the program is nested inside the while loop and then nested again into an if statement. If I
- * were to put a simple "continue;" inside of the if statement, the loop would continue inside the
- * nested for loop rather than going all the way out of the for loop and outside the while loop. */
- mainloop:
- while (pCheck == true) {
- /* I needed to set the variables back to 0 inside the while loop other wise the checks would keep
- * being added up if the program needs to go back to the beginning of the while loop. */
- upCheck = 0;
- loCheck = 0;
- nrCheck = 0;
- syCheck = 0;
- upCheck2 = 0;
- loCheck2 = 0;
- nrCheck2 = 0;
- syCheck2 = 0;
- // I created a for loop that is long enough to iterate through every single character of the password.
- for (int i = 0; i < password.length() - 1; i++) {
- /* The next four statements test to see if the password is the right length, there is no
- * whitespace, it does not contain the last two digits in the birth year and that it does not
- * contain the beginning part of the email. If any of these are incorrect, it prompts the user to
- * enter a new password */
- if (password.length() < 8 || password.length() > 12) {
- System.out.println("Invalid!");
- System.out.printf("%-51s", "Your password needs to be between 8 and 12 characters:");
- password = userInput.nextLine();
- //The next line is the part that allows me to go back to the "mainloop:" I was talking about earlier.
- continue mainloop;
- }
- if (Character.isWhitespace(password.charAt(i))) {
- System.out.println("Invalid!");
- System.out.printf("%-51s", "Please enter a password without white spaces:");
- password = userInput.nextLine();
- continue mainloop;
- }
- if (password.contains(birth2)) {
- System.out.println("Invalid!");
- System.out.printf("%-51s", "Your password cannot contain your birth year:");
- password = userInput.nextLine();
- continue mainloop;
- }
- if (password.toLowerCase().contains(username.toLowerCase())) {
- System.out.println("Invalid!");
- System.out.printf("%-51s", "Your password cannot contain part of your email:");
- password = userInput.nextLine();
- continue mainloop;
- }
- /* I added if statements that check if the character that is being checked is upper case,
- * lower case, a number or a symbol.
- */
- if (Character.isUpperCase(password.charAt(i))) {
- upCheck++;
- /* If the character is anything other than the afformentioned check, then I add one to the
- * second check that checks to see how strong the password is. */
- if (!(Character.isUpperCase(password.charAt(i + 1)))) {
- upCheck2++;
- }
- }
- if (Character.isLowerCase(password.charAt(i))) {
- loCheck++;
- if (!(Character.isLowerCase(password.charAt(i + 1)))) {
- loCheck2++;
- }
- }
- if (Character.isDigit(password.charAt(i))) {
- nrCheck++;
- if (!(Character.isDigit(password.charAt(i + 1)))) {
- nrCheck2++;
- }
- }
- if (password.charAt(i) >= 33 && password.charAt(i) <= 47 || password.charAt(i) >= 58 && password.charAt(i) <= 64 || password.charAt(i) >= 91 && password.charAt(i) <= 96 || password.charAt(i) >= 123) {
- syCheck++;
- if (!(password.charAt(i + 1) >= 33 && password.charAt(i + 1) <= 47 || password.charAt(i + 1) >= 58 && password.charAt(i + 1) <= 64 || password.charAt(i + 1) >= 91 && password.charAt(i + 1) <= 96 || password.charAt(i + 1) >= 123)) {
- syCheck2++;
- }
- }
- }
- // I closed the scanner because the yellow caution thing was bothering me.
- userInput.close();
- // After the for loop is done, this means that all the password checks have been done, so I can stop the while loop now.
- pCheck = false;
- // This last part of the program checks to see how strong the password is and prints out accordingly.
- if (upCheck >= 2 && loCheck >= 2 && nrCheck >= 2 && syCheck >= 2 && upCheck2 >= 2 && loCheck2 >= 2 && nrCheck2 >= 2 && syCheck2 >= 2) {
- System.out.println("Your password is strong");
- }
- if (upCheck >= 2 && syCheck >= 1) {
- System.out.println("Your password is average");
- } else {
- System.out.println("Your password is weak");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement