0

Been stuck on this problem for a while and would appreciate some input. I want to validate a date from user input so that i can perform a calculation with the LocalDate object. however, when a valid date is entered, the date returned is the previous invalid one, which throws an exception and crashes. What am i missing or doing wrong.

public static void main(String[] args) {            
    Scanner sc = new Scanner(System.in);

    // Accept two dates from the user and return the number of days between the two dates
    numDaysBetween(sc); // calls to the inputDate are inside    

    sc.close();     

} // end main   

public static int[] inputDate(Scanner sc) {     
    System.out.print("Enter Date - In format dd/mm/yyyy: ");        

    while(!sc.hasNext("([0-9]{2})/([0-9]{2})/([0-9]){4}")) {
        System.out.print("That's not a valid date. Enter the date again: ");
        sc.nextLine();                 
    } // end while      
    String dateAsString = sc.nextLine();
    String[] dateArr = dateAsString.split("/");

    int[] dateArrInt = Arrays.asList(dateArr)
            .stream()
            .mapToInt(Integer::parseInt)
            .toArray();
    System.out.println(Arrays.toString(dateArrInt));
    try {
        //LocalDate date = LocalDate.of(dateArrInt[2], dateArrInt[1], dateArrInt[0]);
        LocalDate d = LocalDate.of(Integer.parseInt(dateArr[2]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[0]));
        //System.out.println(d.getDayOfMonth() + "/" + d.getMonthValue() + "/" + d.getYear() );

    } catch(DateTimeException e) {                  
        System.out.print(e.getMessage() + "\n");    
        inputDate(sc);

    } // end catch

    return dateArrInt;
} // end inputDate()
Lex Silva
  • 9
  • 1
  • 3
  • Are you doing that to learn about regexes, or do you want any possibility to parse a LocalDate? If the latter, check this out: https://stackoverflow.com/questions/8746084/string-to-localdate – Filip Malczak Apr 08 '18 at 13:33
  • 1
    Why not just use `LocalDate.parse(text)` (or any other parser of your liking) and catch and output exception if the text is incorrectly formatted? – Oleg Sklyar Apr 08 '18 at 13:35
  • Err, your main method doesn't do anything other than creating a Scanner and then closing it. Once it actually calls the inputDate method, the right values are being printed. So you're not running the code you posted. Voting to close. – JB Nizet Apr 08 '18 at 13:36
  • JB Nizet, sorry there, i updated the main. I didnt post the other methods as this is the validation method is giving me an issue. – Lex Silva Apr 08 '18 at 13:43

2 Answers2

3

The right way to get local date from a string is using DateTimeFormatter

    String str = "24/09/2017";
    DateTimeFormatter dt = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate date = LocalDate.parse(str, dt);
2

You're calling the method recursively, but you ignore what it returns. Replace

System.out.print(e.getMessage() + "\n");    
inputDate(sc);

by

System.out.print(e.getMessage() + "\n");    
return inputDate(sc);

But really, you shouldn't reinvent LocalDate parsing. Use the classes provided by the Java API to do that. The documentation is your friend.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255