首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用try/catch块验证电子邮件用户名和密码

问使用try/catch块验证电子邮件用户名和密码
EN

Stack Overflow用户
提问于 2022-10-24 04:38:08
回答 1查看 62关注 0票数 1

我正在尝试编写一个代码来验证电子邮件的用户名和密码,该密码至少有一个大写字母、一个小写字母和一个特殊字符。当我运行这个项目时,我会得到“成功的注册”。不管我输入了什么。我相信我的错误在我的尝试/捕捉块中。

代码语言:javascript
复制
    import java.util.Scanner;

    public class UserSignUp {
        public static void main(String[] args) {
            Scanner input1 = new Scanner(System.in);
            Scanner input2 = new Scanner(System.in);
            String username;
            String password;

        try {
            System.out.print("Enter your username: ");
            username = input1.nextLine();
            System.out.print("Enter your password: ");
            password = input2.nextLine();
        }
        catch (IllegalArgumentException e) {
            System.out.println(e);
        }
        System.out.println("Successfully signed up.");
    }

    public static void validateUsername(String username) throws IllegalArgumentException {
        String pattern = ".+@.+\\.com";
        if (username.matches(pattern)) {
            throw new IllegalArgumentException("Invalid username.");
        }
    }

    public static void validatePassword(String password) throws IllegalArgumentException {
        boolean isUppercase = false;
        boolean isLowercase = false;
        boolean isDigit = false;
        boolean hasSpecialCharacter = false;
        char ch = 0;

        for (int i = 0; i < password.length(); i++) {
            ch = password.charAt(i);
            if (Character.isUpperCase(ch)) {
                isUppercase = true;
            } else {
                throw new IllegalArgumentException("Password must contain an uppercase letter.");
            }
            if (Character.isLowerCase(ch)) {
                isLowercase = true;
            } else {
                throw new IllegalArgumentException("Password must contain a lowercase letter.");
            }
            if (Character.isDigit(ch)) {
                isDigit = true;
            } else {
                throw new IllegalArgumentException("Password must contain a digit.");
            }
            if (!Character.isDigit(password.charAt(ch)) && !Character.isLetter(password.charAt(ch))) {
                hasSpecialCharacter = true;
            } else {
                throw new IllegalArgumentException("Password must contain a special character.");
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-24 06:02:05

以下是我的解决方案,解释已经嵌入:

代码语言:javascript
复制
import java.util.Scanner;

public class UserSignUp {
    public static void main(String[] args) {
        // No need for two scanners, one scanner can accept multiple (also
        // multiple-lined) inputs.
        Scanner in = new Scanner(System.in);

        try {
            // Put validation inside the try block.
            // Regarding there are no other use of the variables, it is unnecessary to give
            // the two variables "Method Level Scope" instead of "Block Scope".
            System.out.print("Enter your username: ");
            String username = in.nextLine();
            validateUsername(username);
            System.out.print("Enter your password: ");
            String password = in.nextLine();
            validatePassword(password);
            // Put the println() inside the try block.
            System.out.println("Successfully signed up.");
        } catch (IllegalArgumentException e) {
            System.out.println(e);
        }

    }

    public static void validateUsername(String username) throws IllegalArgumentException {
        String pattern = ".+@.+\\.com";
        // If not matches the pattern,
        if (!username.matches(pattern)) {
            throw new IllegalArgumentException("Invalid username.");
        }
    }

    public static void validatePassword(String password) throws IllegalArgumentException {
        boolean isUppercase = false;
        boolean isLowercase = false;
        boolean isDigit = false;
        boolean hasSpecialCharacter = false;
        char ch = '\0';

        for (int i = 0; i < password.length(); i++) {
            ch = password.charAt(i);
            if (Character.isUpperCase(ch))
                isUppercase = true;
            if (Character.isLowerCase(ch))
                isLowercase = true;
            if (Character.isDigit(ch))
                isDigit = true;
            // ch is already defined as a character.
            if (!Character.isDigit(ch) && !Character.isLetter(ch))
                hasSpecialCharacter = true;
        }

        // Ask for the constraints after read through all characters.
        if (isUppercase != true)
            throw new IllegalArgumentException("Password must contain an uppercase letter.");
        else if (isLowercase != true)
            // Fix the error message of lower-case letters.
            throw new IllegalArgumentException("Password must contain a lowercase letter.");
        else if (isDigit != true)
            throw new IllegalArgumentException("Password must contain a digit.");
        else if (hasSpecialCharacter != true)
            throw new IllegalArgumentException("Password must contain a special character.");

    }
}

希望这个答案对你有帮助,除非有重要的变化,否则请不要回答这个问题,因为有人可能需要它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74176756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档