There is quite a working code that displays the date from what the user entered.

String dateString = scan.nextLine(); String expectedPattern = "dd/MM/yyyy"; SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern); try { Date date = formatter.parse(dateString); System.out.println(dateString); System.out.println(date); } catch (ParseException e) { e.printStackTrace(); } 

But I can't understand why when I write this code inside the case

 int choice = scan.nextInt(); switch (choice) { case 1: 

Then it stops working and gives an error java.text.ParseException: Unparseable date: "" . What I need is that when a certain case is selected, the user can enter a date, which is then written to the array. Maybe this can be done in some other way?

  • one
    Your date is passed empty. Use debager and you will immediately see why dateString is empty for you - Andrew Bystrov
  • @AndrewBystrov But it’s just because it works, if you don’t insert it into a case. Could you explain in more detail? - Marqiz
  • This is not possible for those pieces that you provided. But most likely after int choice = scan.nextInt (); the scanner cursor is at the end of the string and dateString = scan.nextLine (); reads an empty line. This is only a guess. Scanner never used. I do not know exactly how it works. - Sergey

0