Fix try catch

Pls hepl me fix that try { LocalDateTime today = LocalDateTime.parse(events.getFirst(), DateTimeFormatter.ofPattern(dft)); System.out.println("--- Statistics ---"); System.out.println("Total events: " + events.size()); System.out.println("Events today: " + today.format(date)); System.out.println("First Event: " + events.get(0)); } catch (DateTimeException e){ System.out.println("Error date formatting: " + e.getMessage()); }
13 Replies
JavaBot
JavaBot6d ago
This post has been reserved for your question.
Hey @Dexter! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
ayylmao123xdd
ayylmao123xdd6d ago
well whats the problem
Dexter
DexterOP6d ago
Not print Events Error date formatting: Text 'g 26/11/2025 18:25' could not be parsed at index 0 I can give full code
Dexter
Dexter6d ago
import java.io.*; import java.time.DateTimeException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Scanner;
public class EventLogger {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<String> events = new ArrayList<>();

System.out.print("Write event: ");
String event = input.nextLine();

DateTimeFormatter date = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
String dft = date.format(LocalDateTime.now());


try(BufferedWriter writer = new BufferedWriter(new FileWriter("events.txt", true))) {writer.write(event + " " + dft + "\n");
System.out.println("Event: " + event + " " + dft);
} catch (IOException e){
System.out.println("Error writing to file" + e.getMessage());
}

try(BufferedReader br = new BufferedReader(new FileReader("events.txt"))){
String line;
while ((line = br.readLine()) != null){
events.add(line);
}
} catch (IOException e){
System.out.println("Error reading file" + e.getMessage());
}

try {
LocalDateTime today = LocalDateTime.parse(events.getFirst(), DateTimeFormatter.ofPattern(dft));
System.out.println("--- Statistics ---");
System.out.println("Total events: " + events.size());
System.out.println("Events today: " + today.format(date));
System.out.println("First Event: " + events.get(0));
} catch (DateTimeException e){
System.out.println("Error date formatting: " + e.getMessage());
}

}
}
public class EventLogger {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<String> events = new ArrayList<>();

System.out.print("Write event: ");
String event = input.nextLine();

DateTimeFormatter date = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
String dft = date.format(LocalDateTime.now());


try(BufferedWriter writer = new BufferedWriter(new FileWriter("events.txt", true))) {writer.write(event + " " + dft + "\n");
System.out.println("Event: " + event + " " + dft);
} catch (IOException e){
System.out.println("Error writing to file" + e.getMessage());
}

try(BufferedReader br = new BufferedReader(new FileReader("events.txt"))){
String line;
while ((line = br.readLine()) != null){
events.add(line);
}
} catch (IOException e){
System.out.println("Error reading file" + e.getMessage());
}

try {
LocalDateTime today = LocalDateTime.parse(events.getFirst(), DateTimeFormatter.ofPattern(dft));
System.out.println("--- Statistics ---");
System.out.println("Total events: " + events.size());
System.out.println("Events today: " + today.format(date));
System.out.println("First Event: " + events.get(0));
} catch (DateTimeException e){
System.out.println("Error date formatting: " + e.getMessage());
}

}
}
This message has been formatted automatically. You can disable this using /preferences.
Dexter
DexterOP6d ago
pls help fix because i dont know how fix it i asced gemini,gpt and grok but nothing Event: пвап 26/11/2025 19:40 Error date formatting: Text 'g 26/11/2025 18:25' could not be parsed at index 0 (print)
Madjosz
Madjosz6d ago
Well there is a line in your events.txt which does not conform to your datetime formatter pattern.
Dexter
DexterOP6d ago
There seems to be an exception that ignores this, did I write it or is something wrong?
Madjosz
Madjosz6d ago
Yes, it tries to parse the first line of the file, fails, and logs the error message.
Dexter
DexterOP6d ago
I just don't know what to do anymore, it doesn't work, the AI ​​doesn't help, it just writes nonsense
Madjosz
Madjosz6d ago
The problem is that you store [event name] [event date] in the file but try to parse the whole line as the date. So you need to extract the date from the line again. Since your date is always 16 characters long, you could substring the last 16 characters from the line.
String firstEvent = events.getFirst();
LocalDateTime today = LocalDateTime.parse(firstEvent.substring(firstEvent.length - 16), date);
String firstEvent = events.getFirst();
LocalDateTime today = LocalDateTime.parse(firstEvent.substring(firstEvent.length - 16), date);
Ah and DateTimeFormatter.ofPattern(dft) is wrong. dft is not a pattern but the formatted today.
Dexter
DexterOP6d ago
thanks
JavaBot
JavaBot6d ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
JavaBot
JavaBot6d ago
Post Closed
This post has been closed by <@1206629348526788638>.

Did you find this page helpful?