Here’s my code:
// check if the time within 10 mins
if (Duration.between(listOfAppointment.get(Integer.parseInt(indicator) - 1).getDateTimeOfAppointment(), LocalDateTime.now()).compareTo(Duration.ofMinutes(10)) < 0) {
try {
listOfAppointment.get(Integer.parseInt(indicator) - 1).setCheckIn(true);
System.out.println("The check-in has been done! Let's see your PT!");
break;
} catch (Exception e) {
System.out.println("Invalid input, please try again!");
indicator = console.nextLine();
}
} else {
System.out.println("It seems that it's still too early, please wait util 10 minutes left!");
break;
}
All related packages imported and I tried with sample date like one day after but still it always printed out the try-block content, please help me resolve it. Thanks!
>Solution :
It seems that you’re trying to check if the time of an appointment is within 10 minutes from the current time, and if so, set the check-in flag to true, isn’t it?. However, there is an issue in your code. You are using a try-catch block unnecessarily and breaking the loop within the try block. Here is what I believe to be a corrected version of your code:
// Assuming you have already parsed the indicator as an integer
int index = Integer.parseInt(indicator) - 1;
LocalDateTime appointmentTime = listOfAppointment.get(index).getDateTimeOfAppointment();
LocalDateTime now = LocalDateTime.now();
Duration timeDifference = Duration.between(appointmentTime, now);
if (timeDifference.compareTo(Duration.ofMinutes(10)) < 0) {
listOfAppointment.get(index).setCheckIn(true);
System.out.println("The check-in has been done! Let's see your PT!");
} else {
System.out.println("It seems that it's still too early, please wait until 10 minutes are left!");
}