Checking letter case (Upper/Lower) within a string in Java

后端 未结 9 520
闹比i
闹比i 2020-12-04 10:09

The problem that I am having is that I can\'t get my Password Verification Program to check a string to ensure that, 1 of the characters is in upper case and one is in lower

相关标签:
9条回答
  • 2020-12-04 10:32

    Although this code is likely beyond the understanding of a novice, it can be done in one line using a regex with positive and negative look-aheads:

    boolean ok = 
        password.matches("^(?=.*[A-Z])(?=.*[!@#$%^&*])(?=.*\\d)(?!.*(AND|NOT)).*[a-z].*");
    
    0 讨论(0)
  • 2020-12-04 10:32
    function TellFirstCharacterType(inputString){
        var firstCharacter = inputString[0];
        if(isNaN(firstCharacter)){
            if(firstCharacter == firstCharacter.toUpperCase()){
                return "It's a uppercase character";
            }
            else{
                return "It's a lowercase character";
            }
        }
        else{
            return "It's a Number";
        }
    }
    
    0 讨论(0)
  • 2020-12-04 10:33
    package passwordValidator;
    
    import java.util.Scanner;
    
    public class Main {
        /**
         * @author felipe mello.
         */
    
        private static Scanner scanner = new Scanner(System.in);
    
         /*
         * Create a password validator(from an input string) via TDD
         * The validator should return true if
         *  The Password is at least 8 characters long
         *  The Password contains uppercase Letters(atLeastOne)
         *  The Password contains digits(at least one)
         *  The Password contains symbols(at least one)
         */
    
    
        public static void main(String[] args) {
            System.out.println("Please enter a password");
            String password = scanner.nextLine();   
    
            checkPassword(password);
        }
        /**
         * 
         * @param checkPassword the method check password is validating the input from the the user and check if it matches the password requirements
         * @return
         */
        public static boolean checkPassword(String password){
            boolean upperCase = !password.equals(password.toLowerCase()); //check if the input has a lower case letter
            boolean lowerCase = !password.equals(password.toUpperCase()); //check if the input has a CAPITAL case letter
            boolean isAtLeast8 = password.length()>=8;                    //check if the input is greater than 8 characters
            boolean hasSpecial = !password.matches("[A-Za-z0-9]*");       // check if the input has a special characters
            boolean hasNumber = !password.matches(".*\\d+.*");            //check if the input contains a digit
            if(!isAtLeast8){
                System.out.println("Your Password is not big enough\n please enter a password with minimun of 8 characters");
                return true;
            }else if(!upperCase){
                System.out.println("Password must contain at least one UPPERCASE letter");
                return true;
            }else if(!lowerCase){
                System.out.println("Password must contain at least one lower case letter");
                return true;
            }else if(!hasSpecial){
                System.out.println("Password must contain a special character");
                return true;
            }else if(hasNumber){
                System.out.println("Password must contain at least one number");
                return true;
            }else{
                System.out.println("Your password: "+password+", sucessfully match the requirements");
                return true;
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-04 10:42

    That's what I got:

        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a nickname!");
        while (!scanner.hasNext("[a-zA-Z]{3,8}+")) {
            System.out.println("Nickname should contain only Alphabetic letters! At least 3 and max 8 letters");
            scanner.next();
        }
        String nickname = scanner.next();
        System.out.println("Thank you! Got " + nickname);
    

    Read about regex Pattern here: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

    0 讨论(0)
  • 2020-12-04 10:51

    I have streamlined the answer of @Quirliom above into functions that can be used:

    private static boolean hasLength(CharSequence data) {
        if (String.valueOf(data).length() >= 8) return true;
        else return false;
    }
    
    private static boolean hasSymbol(CharSequence data) {
        String password = String.valueOf(data);
        boolean hasSpecial = !password.matches("[A-Za-z0-9 ]*");
        return hasSpecial;
    }
    
    private static boolean hasUpperCase(CharSequence data) {
        String password = String.valueOf(data);
        boolean hasUppercase = !password.equals(password.toLowerCase());
        return hasUppercase;
    }
    
    private static boolean hasLowerCase(CharSequence data) {
        String password = String.valueOf(data);
        boolean hasLowercase = !password.equals(password.toUpperCase());
        return hasLowercase;
    }
    
    0 讨论(0)
  • 2020-12-04 10:51

    A quick look through the documentation on regular expression sytanx should bring up ways to tell if it contains a lower/upper case character at some point.

    0 讨论(0)
提交回复
热议问题