Advertisement
Guest User

Untitled

a guest
Jun 4th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.54 KB | None | 0 0
  1. // I imported the Scanner class so that I can accept userInput
  2. import java.util.Scanner;
  3.  
  4. public class Test {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         //****INITIALIZING VARIABLES****//
  9.        
  10.         /* Variables are initialized up here so that they can easily be found and changed for tests.
  11.          * I initialized the Scanner and assigned it to a variable name "userInput" to make it usable in the
  12.          * rest of the program. */
  13.         Scanner userInput = new Scanner(System.in);
  14.         /* These checks will be referred upon to check how many times the password contains an upper case
  15.          * letter(upCheck), a lower case letter(loCheck), a number(nrCheck) and a symbol(syCheck).*/
  16.         int upCheck, loCheck, nrCheck, syCheck;
  17.        
  18.         /* These checks will be referred upon when checking how many times the appropriate character is
  19.          * followed by a different character making the password stronger.*/
  20.         int upCheck2, loCheck2, nrCheck2, syCheck2;
  21.        
  22.         //****OBTAINING INFORMATION FROM USER****//
  23.        
  24.         /* I prompt the user to enter his/her email address which will be stored in the variable "email".
  25.          * This is so that I can check whether or not the password contains part of the email address. */
  26.         System.out.printf("%-51s", "Enter your email address");
  27.         String email = userInput.nextLine();
  28.         /* I grab the index of the @ sign in the email which is used as the last parameter in obtaining
  29.          * the user name of the user through the substring method.*/
  30.         int atSign = email.indexOf('@');
  31.         /* If there is no @ symbol in the email, the variable atSign will return -1. This means that
  32.          * obtaining the user name of the email will be impossible. I fix this by figuring out of
  33.          * atSign is -1 and if it is, it will continually prompt the user until they enter a valid
  34.          * email address containing the @ symbol.*/
  35.         while (atSign == -1) {
  36.             System.out.printf("%-51s", "Please enter a valid email address");
  37.             email = userInput.nextLine();
  38.             atSign = email.indexOf('@');
  39.         }
  40.         String username = email.substring(0, atSign);
  41.         /* I then prompt the user to enter his/her year of birth. Later on in the program, I will be testing
  42.          * to see if the password contains the last two digits of the user's year of birth. */
  43.         System.out.printf("%-51s", "Enter your year of birth");
  44.         String birth = userInput.nextLine();
  45.         /* 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
  46.          * continue to prompt the user for a proper birth year.*/
  47.         while (birth.length() != 4) {
  48.             System.out.printf("%-51s", "Please enter a valid birth year");
  49.             birth = userInput.nextLine();
  50.         }
  51.         /* The next variable pulls the last two numbers from the user's birth year. The last two numbers are
  52.          * needed to check if they are in the password, which would make the password Invalid.class */
  53.         String birth2 = birth.substring(2, 4);
  54.         /* The last prompt asks the user for their password. Without this piece of information, this program
  55.          * would not exist */
  56.         System.out.printf("%-51s", "Enter your password");
  57.         String password = userInput.nextLine();
  58.        
  59.         //****PASSWORD VERIFICATION****//
  60.        
  61.         // Sets the password check to true to make sure that the following while loop actually runs
  62.         boolean pCheck = true;
  63.         /* The "mainloop:" is used as a check point that the program can refer back to. There is many times
  64.          * when the program is nested inside the while loop and then nested again into an if statement. If I
  65.          * were to put a simple "continue;" inside of the if statement, the loop would continue inside the
  66.          * nested for loop rather than going all the way out of the for loop and outside the while loop. */
  67.         mainloop:
  68.         while (pCheck == true) {
  69.             /* I needed to set the variables back to 0 inside the while loop other wise the checks would keep
  70.              * being added up if the program needs to go back to the beginning of the while loop. */
  71.             upCheck = 0;
  72.             loCheck = 0;
  73.             nrCheck = 0;
  74.             syCheck = 0;
  75.            
  76.             upCheck2 = 0;
  77.             loCheck2 = 0;
  78.             nrCheck2 = 0;
  79.             syCheck2 = 0;
  80.            
  81.             // I created a for loop that is long enough to iterate through every single character of the password.
  82.             for (int i = 0; i < password.length() - 1; i++) {
  83.                 /* The next four statements test to see if the password is the right length, there is no
  84.                  * whitespace, it does not contain the last two digits in the birth year and that it does not
  85.                  * contain the beginning part of the email. If any of these are incorrect, it prompts the user to
  86.                  * enter a new password */
  87.                 if (password.length() < 8 || password.length() > 12) {
  88.                     System.out.println("Invalid!");
  89.                     System.out.printf("%-51s", "Your password needs to be between 8 and 12 characters:");
  90.                     password = userInput.nextLine();
  91.                     //The next line is the part that allows me to go back to the "mainloop:" I was talking about earlier.
  92.                     continue mainloop;
  93.                 }
  94.                 if (Character.isWhitespace(password.charAt(i))) {
  95.                     System.out.println("Invalid!");
  96.                     System.out.printf("%-51s", "Please enter a password without white spaces:");
  97.                     password = userInput.nextLine();
  98.                     continue mainloop;
  99.                 }
  100.                 if (password.contains(birth2)) {
  101.                     System.out.println("Invalid!");
  102.                     System.out.printf("%-51s", "Your password cannot contain your birth year:");
  103.                     password = userInput.nextLine();
  104.                     continue mainloop;
  105.                 }
  106.                 if (password.toLowerCase().contains(username.toLowerCase())) {
  107.                     System.out.println("Invalid!");
  108.                     System.out.printf("%-51s", "Your password cannot contain part of your email:");
  109.                     password = userInput.nextLine();
  110.                     continue mainloop;
  111.                 }
  112.                
  113.                 /* I added if statements that check if the character that is being checked is upper case,
  114.                  * lower case, a number or a symbol.
  115.                  */
  116.                 if (Character.isUpperCase(password.charAt(i))) {
  117.                     upCheck++;
  118.                     /* If the character is anything other than the afformentioned check, then I add one to the
  119.                      * second check that checks to see how strong the password is. */
  120.                     if (!(Character.isUpperCase(password.charAt(i + 1)))) {
  121.                         upCheck2++;
  122.                     }
  123.                 }
  124.                 if (Character.isLowerCase(password.charAt(i))) {
  125.                     loCheck++;
  126.                     if (!(Character.isLowerCase(password.charAt(i + 1)))) {
  127.                         loCheck2++;
  128.                     }
  129.                 }
  130.                 if (Character.isDigit(password.charAt(i))) {
  131.                     nrCheck++;
  132.                     if (!(Character.isDigit(password.charAt(i + 1)))) {
  133.                         nrCheck2++;
  134.                     }
  135.                 }
  136.                 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) {
  137.                     syCheck++;
  138.                     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)) {
  139.                         syCheck2++;
  140.                     }
  141.                 }
  142.             }
  143.             // I closed the scanner because the yellow caution thing was bothering me.
  144.             userInput.close();
  145.             // After the for loop is done, this means that all the password checks have been done, so I can stop the while loop now.
  146.             pCheck = false;
  147.             // This last part of the program checks to see how strong the password is and prints out accordingly.
  148.             if (upCheck >= 2 && loCheck >= 2 && nrCheck >= 2 && syCheck >= 2 && upCheck2 >= 2 && loCheck2 >= 2 && nrCheck2 >= 2 && syCheck2 >= 2) {
  149.                 System.out.println("Your password is strong");
  150.             }
  151.             if (upCheck >= 2 && syCheck >= 1) {
  152.                 System.out.println("Your password is average");
  153.             } else {
  154.                 System.out.println("Your password is weak");
  155.             }
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement