简体   繁体   English

java - 如何检查用户是否在java中的字符串中输入了正确的字符?

[英]How to check if a correct character was entered by a user in a string in java?

I'm trying to write a code that would let me check if the item inputted by the user is valid.我正在尝试编写一个代码,让我检查用户输入的项目是否有效。 The item has a format of: NNN-LL-NNNNNN, where N is a number and L is a letter.该项目的格式为:NNN-LL-NNNNNN,其中 N 是数字,L 是字母。

My code should be able to determine whether the item is valid by checking all the characters in the string, so if for example the user inputs '222-DN-1055' it is valid but if it's '2-DN-1055' then it is not.我的代码应该能够通过检查字符串中的所有字符来确定该项目是否有效,因此,例如,如果用户输入“222-DN-1055”它是有效的,但如果它是“2-DN-1055”那么它不是。

I can only use java methods I learnt in my course that's why I'm trying to do it with a String Buffer.我只能使用我在课程中学到的 java 方法,这就是我尝试使用字符串缓冲区的原因。 I can't use regex.我不能使用正则表达式。

Now, this is the code I have done so far:现在,这是我到目前为止所做的代码:

public class ItemChecker{

    //vars
    private String userInput;
    private StringBuffer strBuff;
    private String validity;

    //constructor
    public ItemChecker(){
            strBuff=new StringBuffer();
    }

    //set
    public void setUserInput(String userInput){
            this.userInput=userInput;
    }

    //compute
    public void computeValidity(){
        for(int i=0;i<userInput.length();i++){
            if (Character.isDigit(userInput.charAt(0))){
                strBuff.append(userInput.charAt(0));
            }
            else if (Character.isDigit(userInput.charAt(1))){
                strBuff.append(userInput.charAt(1));
                }
            else if (Character.isDigit(userInput.charAt(2))){
                strBuff.append(userInput.charAt(2));
                }
            else if (userInput.charAt(3)=='-'){
                strBuff.append(userInput.charAt(3));
                }
            else if (Character.isLetter(userInput.charAt(4))){
                strBuff.append(userInput.charAt(4));
                }
            else if (userInput.charAt(4) == 'c' || userInput.charAt(i) == 'd'|| userInput.charAt(i) == 'g' || userInput.charAt(i) == 'k' || userInput.charAt(i) == 'l' || userInput.charAt(i) == 'm'|| userInput.charAt(i) == 'o'|| userInput.charAt(i) == 'r' || userInput.charAt(i) == 's' || userInput.charAt(i) == 't' || userInput.charAt(i) == 'w'){
                strBuff.append(userInput.charAt(4));
                }
            else if (Character.isLetter(userInput.charAt(5))){
                strBuff.append(userInput.charAt(5));
                }
            else if (userInput.charAt(5) == 'k' || userInput.charAt(i) == 'e' || userInput.charAt(i) == 'n' || userInput.charAt(i) == 'w' || userInput.charAt(i) == 'l' || userInput.charAt(i) == 'y' || userInput.charAt(i) == 'd'|| userInput.charAt(i) == 'h' || userInput.charAt(i) == 'm' || userInput.charAt(i) == 's' || userInput.charAt(i) == 'o' || userInput.charAt(i) == 'x'){
                strBuff.append(userInput.charAt(5));
                }
            else if (userInput.charAt(6)=='-'){
                strBuff.append(userInput.charAt(6));
                }
            else if (Character.isDigit(userInput.charAt(7))){
                strBuff.append(userInput.charAt(7));
                }
            else if (Character.isDigit(userInput.charAt(8))){
                strBuff.append(userInput.charAt(8));
                }
            else if (Character.isDigit(userInput.charAt(9))){
                strBuff.append(userInput.charAt(9));
                }
            else if (Character.isDigit(userInput.charAt(10))){
                strBuff.append(userInput.charAt(10));
                }
            else if (Character.isDigit(userInput.charAt(11))){
                strBuff.append(userInput.charAt(11));
                }
            else if (Character.isDigit(userInput.charAt(12))){
                strBuff.append(userInput.charAt(12));
            }
            else{
                strBuff.append("Your registration plate is not valid.");
            }
            }

        validity=strBuff.toString();
    }

    //get
    public String getValidity(){
            return validity;
    }
}

The code does not really work and I have no clue how to proceed from here.该代码并没有真正起作用,我不知道如何从这里开始。 Also how do I make sure that if a user inputs more than six numbers at the end, the code would be considered invalid as well.另外,我如何确保如果用户最后输入的数字超过六个,该代码也将被视为无效。

here is a cleaned up version of your code, though its not complete.这是您的代码的清理版本,尽管它不完整。 You can follow the style of the code to complete the rest of the logic.你可以按照代码的风格来完成剩下的逻辑。

public void computeValidity(){
        if (userInput.length() < 8 && userInput.length()> 13){
            //if length is not in between 8 to 13
            return; //exits function
        }
        for(int i=0;i<userInput.length();i++) {
            char c = userInput.charAt(i);
            if (i <= 2) {
                if (Character.isDigit(c)) {
                    //do whatever for true
                } else {
                    //return false etc
                }
            } else if (i == 3 || i == 6) {
                if (c == '-') {
                    //do whatever for true
                } else {
                    //return false etc
                }
            } else if (i <= 5) {
                //check for county identifiers
            } else {
                //finally check for digits
                if (Character.isDigit(c)) {

                } else {

                }
            }
        }
    }

I'd suggest that you can try to split the userInput .我建议您可以尝试拆分userInput Solve the problem piece by piece.逐条解决问题。

I use Scanner for this example我在这个例子中使用Scanner

    Scanner scan = new Scanner(System.in);
    String userInputPlate; 

    while (true) {
        userInputPlate = scan.nextLine();

        if (userInputPlate.equalsIgnoreCase("end")) {
            break;
        }

        // add the condition for 'SSSSSS'
        // i.e. > 8 
        // I omit it for readability
        if (userInputPlate.length() <= 13) {
            System.out.println("Please try again");
        }
    }
        String[] plateParts = userInputPlate.split("-");

        String YYY = plateParts[0];
        String LL = plateParts[1];
        String SSSSSS = plateParts[2];

        // notice I use "!" in if statement
        for (int i = 0; i < YYY.length(); i++){
            char[] YYYchar = YYY.toCharArray();
            if (!Character.isDigit((YYYchar[i]))){
                return;
            }
        }

        // for loop for LL, I skip it

        // do the same SSSSSS
        for (int i = 0; i < SSSSSS.length(); i++){
            char[] SSSSSSchar = SSSSSS.toCharArray();
                if(!Character.isDigit(SSSSSSchar[i])){
                    return;
                }
            }
        }

By doing so, you not only can debug with each segment easier, and it also improves the readability.通过这样做,您不仅可以更轻松地调试每个段,还可以提高可读性。

Hope this works for you.希望这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM