0

In the class named Source create the following public static method:

checkRegistrationNumber(String):int

The input parameter is a vehicle registration number and the output is -1, 0 or 1 based on below given rules. If the Vehicle registration number is valid as per the below format the method should return 0, else -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.

Do the following in the main method of Source class

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

I tried this code.

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");
    }
 }
}

but When checking DL10G4839, it is showing Invalid even it is lucky reg. number. it is not working properly.

ran
  • 19
  • 9
  • 1
    Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Turing85 May 03 '21 at 17:22
  • `DL10G4839` doesn't even pass `[(KA)(DL)][(0[1-9])(10)][A-Z]{1,2}[1-9]\\d{3}` test https://regex101.com/r/KARISl/1 – SkillGG May 03 '21 at 17:33
  • Since you need to extract the individual values anyway, you could ditch the regex completely. Rather parse the String according to your given pattern. Also, your regex doesn't even compile without an error. Even the part `[(KA)(DL)]` is already wrong. – maio290 May 03 '21 at 17:34
  • 2
    This should be your Regular Expression: `(KA|DL)(10|0[1-9])([A-Z]{1,2})([1-9][0-9]{3})` – SkillGG May 03 '21 at 17:37
  • @SkillGG You should probably add the the start and end of string anchors ;) – maio290 May 03 '21 at 17:39

1 Answers1

4

Your regular expression is wrong!

If you want to check for the constraints that are in this exercise you should use this regex:

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

Explanation:

^ # 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

The rest of the code is kinda sloppy, and could be improved, but works so I guess we can leave it as is.

Edit: If you don't care about groups and won't use them (it won't be shorter though), then you could get rid of them making it:

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