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 {

                }
            }
        }
    }

Java相关问答推荐

ittext pdf延迟签名,签名无效

给定Java枚举类,通过值查找枚举

存根基类的受保护方法

名称冲突具有相同的擦除

如何创建同一类的另一个对象,该对象位于变量中?

Spring data JPA/Hibernate根据id获取一个列值

如何使用SpringBoot中的可分页对整数作为字符串存储在数据库中时进行排序

将响应转换为带值的键

通过Spring Security公开Spring Boot执行器端点

类型集合的Jackson JsonNode:类型引用的对象读取器应该是Singleton吗?

声明带有泛型的函数以用作查找映射中的值

支持MySQL 5.6的最新Hibernate版本

Android Java:已设置但未读取SharedPreferences

如何在IntelliJ IDEA的Build.sbt中添加外部JAR文件?

基于Java中mm/dd/yy格式的最近日期对数组列表进行排序

无法使用Open WebStart Java 8运行jnlp

java.lang.ClassCastException:com.google.firebase.FirebaseException无法转换为com.google.fire base.auth.FirebaseAuthException

如何使用stream.allMatch()为空流返回false?

有没有办法仅将 JComboBox 中的选定项目居中(因此保持组合框中的所有项目左对齐)

测试使用 Room 和 RxJava 实现的数据库时出现java.lang.AssertionError:值不存在