Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Please help me with this password validation - While Loop Running Once

Please help me with this password validation - While Loop Running Once

Scheduled Pinned Locked Moved Java
questionjavahelp
8 Posts 4 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    R1s1ng Phoen1x
    wrote on last edited by
    #1

    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;
                }
            }
        }
    }
    

    }

    L 2 Replies Last reply
    0
    • R R1s1ng Phoen1x

      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;
                  }
              }
          }
      }
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      R 1 Reply Last reply
      0
      • R R1s1ng Phoen1x

        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;
                    }
                }
            }
        }
        

        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • L Lost User

          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.

          R Offline
          R Offline
          R1s1ng Phoen1x
          wrote on last edited by
          #4

          Ok... I will try that, I thought you could check for multiple conditions in that way as I saw it done that way somewhere.

          L E 2 Replies Last reply
          0
          • L Lost User

            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

            R Offline
            R Offline
            R1s1ng Phoen1x
            wrote on last edited by
            #5

            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.

            T 1 Reply Last reply
            0
            • R R1s1ng Phoen1x

              Ok... I will try that, I thought you could check for multiple conditions in that way as I saw it done that way somewhere.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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 tests

              So when this test fails (as it mostly will) you request a new password, but then set validPassword = true;, without checking anything else.

              1 Reply Last reply
              0
              • R R1s1ng Phoen1x

                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.

                T Offline
                T Offline
                Touchstone Educationals
                wrote on last edited by
                #7

                Thanks for sharing this great information. PTE Coaching Classes in Chandigarh

                1 Reply Last reply
                0
                • R R1s1ng Phoen1x

                  Ok... I will try that, I thought you could check for multiple conditions in that way as I saw it done that way somewhere.

                  E Offline
                  E Offline
                  Ezalhela EzlH
                  wrote on last edited by
                  #8

                  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

                  APK

                  downloads and user data, ensuring that the platform maintains a high level of security for its users.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups