简体   繁体   中英

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.

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.

I can only use java methods I learnt in my course that's why I'm trying to do it with a String Buffer. 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 . Solve the problem piece by piece.

I use Scanner for this example

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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