-1

I've already asked a question about it here, so I decided to do it from scratch one more time. I am trying to validate dd-MMM-yyyy format but for some reason I always get an error.

Here is my code:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 public class Review1_Main{

 private static Pattern DATE_VALIDATION_PATTERN =
      Pattern.compile("^(([1-9])|([0][1-9])|([1-2][0-9])|([3][0-1]))" +
                      "\\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|" +
                      "Nov|Dec)\\-\\d{4}$");

 public static boolean validate(final String date){

 Matcher matcher = DATE_VALIDATION_PATTERN.matcher(date);

 if(matcher.matches()){

   matcher.reset();

   if(matcher.find()){

     String dd = matcher.group(1);
     String mm = matcher.group(2);
     int yy = Integer.parseInt(matcher.group(3));

     if (dd.equals("31") &&  (mm.equals("Apr") || mm.equals("Jun") ||
                              mm.equals("Sep") || mm.equals("Nov"))) {
       return false;
     } else if (mm.equals("Feb")) {

       if(yy % 4==0){
         if(dd.equals("30") || dd.equals("31")){
           return false;
         }else{
          return true;
         }
       }else{
         if(dd.equals("29")||dd.equals("30")||dd.equals("31")){
           return false;
         }else{
           return true;
         }
       }
     }else{
       return true;
     }
   }else{
     return false;
   }
 }else{
   return false;
 }
}

public static void main(String[] args) {

 Scanner s = new Scanner(System.in);
 System.out.print("Enter a date: ");
   String input = s.nextLine();
 System.out.println(validate(input));
}
}

And here is an error I get:

Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:542)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Review1_Main.validate(Review1_Main.java:21)
    at Review1_Main.main(Review1_Main.java:57)

I understand that this error means that my input = null, but I can't fix it. Any advice would be good. I really want to understand it!

Thank you!

Finally found the answer:

private static Pattern DATE_VALIDATION_PATTERN = Pattern.compile("(0?[1-9]|[12][0-9]|3[01])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|" +
                  "Nov|Dec)-((19|20)\\d\\d)");
VLAZ
  • 26,331
  • 9
  • 49
  • 67
John E.
  • 321
  • 4
  • 11
  • Why do you need manual validation? Just use [DateTimeFormatter](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) and catch exception. – S. Kadakov May 24 '16 at 13:04
  • @S.Kadakov I know that I am not allowed to post my "assignments" here, but this is what my teacher wants to get from me, I've already done it with `DateTimeFormatter` – John E. May 24 '16 at 13:06

1 Answers1

1

Basically, the problem is that you are attempting to use a capture group in the regex that didn't match anything in the input string.

I'm not exactly sure, but the either the 3rd capture group is ([0][1-9]) or ([1-2][0-9]), or there isn't one at all. I note that you don't have any round brackets around the "year" section of your regex, so that can't be a capture group under any interpretation!

UPDATE - according to this Q&A: How are nested capturing groups numbered in regular expressions? - capture groups can be nested, and they are numbered according to the order of the respective group's opening bracket.

That means that capture group 3 in your regex is the optional ([0][1-9]) group.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I coped it from [here](http://stackoverflow.com/questions/10931179/need-regular-expression-for-validating-date-in-dd-mmm-yyyy-format) – John E. May 24 '16 at 13:12
  • I'm sure that your teacher will be pleased to hear that :-). There is nothing wrong with the regex you copied, but you can't use it like you are trying to use it! – Stephen C May 24 '16 at 13:16
  • Thank you very-very much, I fixed my problem, you were right! Thanks a lot! – John E. May 24 '16 at 13:17
  • 1
    Can I encourage you to STOP "borrowing" other people's code snippets to do your homework. It is more effort to write your own code from scratch, but you will learn a lot more. And I'm SURE that is what your teacher (and future employers) actually want. – Stephen C May 24 '16 at 13:20