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()