0

Can anyone teach me how can when user type -1 exit the program and password have mistake re-entry the password and I have the big problem for this code I dun know why user type error password but java still show Password is valid but when i use else if that will be only display one thing of my rule thanks

package test;

import java.util.Scanner;

class PasswordValidator {

public static void main(String[] args) {
    while(true) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter user name : ");
    String username = sc.nextLine();
    System.out.print("Enter password : ");


    String password;
    boolean hasLength;
    boolean hasUppercase;
    boolean hasLowercase;
    boolean hasDigit;
    boolean hasSpecial;
    boolean whitespace;
    boolean userpass;


    password = sc.nextLine();
    hasLength = password.length() > 10;
    hasLength = password.length() < 7;
    hasUppercase = !password.equals(password.toUpperCase());
    hasLowercase = !password.equals(password.toLowerCase());
    hasDigit = password.matches(".*[0-9].*");// checks for digits
    hasSpecial = !password.matches("[A-Za-z0-9]*"); // for anything not a letter in the ABC's
    whitespace = !password.matches(".*\\s.*");
    userpass = !password.matches(username);


    if (hasLength) {
        System.out.println("Password should be within 7 to 10 characters in length");
    }
     if (!hasUppercase) {
        System.out.println("Password should be at least one upper-case alphabet");
    }
     if (!hasLowercase) {
        System.out.println("Password should be at least one lower-case alphabet");
    }
     if (!hasDigit) {
        System.out.println("Password should cantain at least one number");
    }
     if (!hasSpecial) {
        System.out.println("Password should be at least one special character");
    }
     if (!whitespace) {
        System.out.println("Password should not contain whitespace.");
    }
     if (!userpass) {           
        System.out.println("Password should not contain or be the same as user name.");
    }       
    else {
        System.out.println("Password is valid.");       
    }   







}}}
  • Why don't you use regex for this? It would be strong and simple code for same stuff. – Pushpesh Kumar Rajwanshi Nov 10 '18 at 19:50
  • As a note, to make your code show up nicely, use 4 spaces of indentation (select and press the `{}` symbol in the editor). – Henning Koehler Nov 10 '18 at 19:51
  • 2
    Possible duplicate of [Regexp Java for password validation](https://stackoverflow.com/questions/3802192/regexp-java-for-password-validation) – Locke Nov 10 '18 at 19:51
  • `password.equals(password.toUpperCase())` does it make sense to you ? A word equals to itself but with all uppercase ? `aZeRty` equals `AZERTY` ? never – azro Nov 10 '18 at 19:51
  • If your question is about control-flow, it's a little unclear. In any case, you'll want to separate your check of whether a password matches your requirements (needed when choosing a new password) from the check of whether it matches the password the user previously defined. – Henning Koehler Nov 10 '18 at 19:55
  • 1
    @azro that check is ok - `aZeRty` does not equal `AZERTY` which is how he checks that the password does contain a *lower-case* character. There is another bug though with the length-check (the first check result is overwritten). – Henning Koehler Nov 10 '18 at 19:57

1 Answers1

0

Regular expressions like as you have at the end can be compressed further.

Here's a link to a good one:

Regexp Java for password validation

Locke
  • 7,626
  • 2
  • 21
  • 41