-1

this code should check the accuracy of the typed date, if it's not accepted, the user should digit a new date, and so on until a correct date is typed. The problem is that after the first try, I type another date but then it returns an error.

    String amPm"", temp="";
    int hours=0,minutes=0,seconds=0;
    boolean flag = true;
    Scanner input1 = new Scanner(System.in);
    input.useDelimiter(":");

    do {
    System.out.println("Insert a date: ");
    hours= input.nextInt();
    minutes = input.nextInt();
    temp = input.nextLine();
    seconds = Integer.parseInt(temp.substring(1, (temp.length() - 3)));
    amPm = temp.substring((temp.length() - 2), temp.length());
    
    if ((hours > 12 || hours < 1) || (minutes > 59 || minutes < 0) || (seconds > 59 || seconds < 0) || 
            !(amPm.equalsIgnoreCase("AM")) || (amPm.equalsIgnoreCase("PM")))
        System.out.println("Error - date not accepted.");
        
    else
        flag = false;
    }
    while(flag);

what am I doing wrong?

mikirug88
  • 49
  • 7
  • What is the error returned ? – Florent Bayle May 28 '21 at 11:32
  • it just says:Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at ProgettiCapitolo3.Differenza_orari.main(Differenza_orari.java:20) java:20 --> hours = input.nextInt(); – mikirug88 May 28 '21 at 11:35

0 Answers0