-1

I would like to create a custom formatter. I would like to enter a string in the form of yyyy-mm-dd-hh so I can use a formatter to generate a LocalDateTime variable. So what I want to do is input a string in the form of yyyy-mm-dd-hh and then I can format that string to a LocalDateTime format.

But I keep getting the error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '' could not be parsed at index 0
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494)
    at Festival.main(Festival.java:28)

My code:

 Scanner sc = new Scanner(System.in);
    String naam;
    String lineup;
    int dagen;
    int artiesten;
    int duurtijd;
    String wanneer;

    System.out.println("Wat is de naam van je festival?");
    naam = sc.next();
    System.out.println("Hoeveel dagen duurt je festival?");
    dagen = sc.nextInt();
    System.out.println("Hoeveel artiesten plan je per dag?");
    artiesten = sc.nextInt();
    System.out.println("Wat is de duurtijd van een optreden (in minuten)?");
    duurtijd = sc.nextInt();
    System.out.println("Wanneer zal je festival doorgaan? geef de startdatum als volgt: jaar-maand-dag-uur");
    wanneer = sc.nextLine();

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH");
    LocalDateTime formatDateTime = LocalDateTime.parse(wanneer, formatter);
    Podium podium = new Podium(naam, formatDateTime, dagen, artiesten, duurtijd);
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Loran Maes
  • 95
  • 7

1 Answers1

0

The answer was simple. It was the problem of the scanner. I just had to use

sc.next()

instead of

sc.nextLine()

Loran Maes
  • 95
  • 7