Extract Error Entries from Logs in Java: Explanation & Practice
Extract errors from log entries
Problem summary
Parse log entries and extract only ERROR level messages.
Starter code
public class Main {
public static void main(String[] args) {
String[] logs = {
"[INFO] Application started",
"[ERROR] Connection failed",
"[DEBUG] Processing request",
"[ERROR] Timeout occurred",
"[INFO] Request completed"
};
// Extract and print only ERROR messages
}
}Expected output and test cases
- Only ERROR messages extracted
Connection failed Timeout occurred
Hints
- Check if line contains [ERROR]
- Extract message after ]
- Use substring or split
Related File I/O exercises
- Practice Simple JSON Handler in Java
- Practice CSV Parser in Java
- Practice Configuration Reader in Java
Practice all File I/O exercises · Run this idea in the Java compiler