【问题标题】:vehicle registration number validator in java [duplicate]java中的车辆登记号验证器[重复]
【发布时间】:2021-07-26 02:43:06
【问题描述】:

在名为 Source 的类中创建以下公共静态方法:

checkRegistrationNumber(String):int

输入参数是车辆登记号,输出是 -1、0 或 1,基于以下给定规则。 如果车辆登记号按照以下格式有效,则该方法应返回 0,否则返回 -1

PPXXQQYYYY
PP - Should be either KA or DL
XX - Number from 01 to 10
QQ - 1 or 2 alphabets from A-Z(uppercase)
YYYY - Number from 1000 to 9999 
Ex: KA01MG2323, DL10G5454

Method should return 1, if the registration number is valid, and last 4 digits add up to a lucky number. 
When last 4 digits are repeatedly added(see below) and the sum is 6, it is a lucky number
KA01MG8484
8+4+8+4 = 24 -> 2 + 4 = 6 (Lucky number)
if the input string is empty or null, the method should return -1.

在Source类的main方法中做如下操作

Accept Registration number from the console
If the Registration number is invalid, display Invalid registration number
If the Registration number is valid but not lucky, display Valid registration number
If the Registration number is valid and lucky, display Lucky registration number

我试过这段代码。

import java.util.*;
import java.util.regex.*;
class Source
{
    static int checkRegistrationNumber(String st){
        String regex= "[(KA)(DL)][(0[1-9])(10)][A-Z]{1,2}[1-9]\\d{3}";
        Pattern p=Pattern.compile(regex);
        Matcher m = p.matcher(st);
        if(m.find()){
             String lastfour="";
             lastfour = st.substring(st.length()-4);
             int a = Integer.parseInt(lastfour);
             int[] arr = new int[10];
             int u = 1000;
             for(int i=0;i<4;i++){
                  arr[i] =a/u;
                  a=a%u;
                  u=u/10;
             }
             int sum;
             sum=arr[0]+arr[1]+arr[2]+arr[3];
             if(sum>10){
                 int sum1=sum/10;
                 int sum2=sum%10;
                 int sum3= sum1+sum2;
                 if(sum3==6){
                    return 1;
                 }
                 else {
                    return 0;
                 }
           }
           else if(sum==6){
               return 1;
            }
        else{
            return 0;
        }
 }
 else{
    return -1;
 }
 }
 public static void main(String[] args)
{
    Scanner sc =new Scanner(System.in);
    String str=sc.nextLine();
    int n=checkRegistrationNumber(str);
    if(n==1){
        System.out.println("Lucky registration number");
    }
    else if(n==0){
        System.out.println("Valid registration number");
    }
    else{
        System.out.println("Invalid registration number");
    }
 }
}

但是当检查DL10G4839时,它显示无效,即使它是幸运的。数字。 它不能正常工作。

【问题讨论】:

  • DL10G4839 甚至没有通过 [(KA)(DL)][(0[1-9])(10)][A-Z]{1,2}[1-9]\\d{3} 测试 regex101.com/r/KARISl/1
  • 由于无论如何您都需要提取单个值,因此您可以完全放弃正则表达式。而是根据您给定的模式解析字符串。此外,您的正则表达式甚至无法在没有错误的情况下编译。就连[(KA)(DL)]这个部分都已经错了。
  • 这应该是你的正则表达式:(KA|DL)(10|0[1-9])([A-Z]{1,2})([1-9][0-9]{3})
  • @SkillGG 您可能应该添加字符串锚点的开头和结尾;)

标签: java arrays regex


【解决方案1】:

你的正则表达式错误!

如果你想检查本练习中的约束,你应该使用这个正则表达式:

"^(KA|DL)(10|0[1-9])([A-Z]{1,2})([1-9][0-9]{3})$"

解释:

^ # start of the string
( # open first group
KA|DL # find KA or DL
) # close first group
( # second group
10| # 10 literally or
0[1-9] # numbers from 01 to 09
) # close second group
( # third group
[A-Z]{1,2} # one or two A-Z (uppercase)
) # close third group
( # open fourth group
[1-9][0-9]{3} # numbers from 1000 to 9999
) # close fourth group
$ # end of the string

其余的代码有点草率,可以改进,但可以工作,所以我想我们可以保持原样。

编辑:如果你不关心组并且不会使用它们(虽然它不会更短),那么你可以摆脱它们来制作它:

"^(?:KA|DL)(?:10|0[1-9])[A-Z]{1,2}[1-9][0-9]{3}$"

【讨论】:

    猜你喜欢
    • 2017-05-20
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多