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

  1. Check if line contains [ERROR]
  2. Extract message after ]
  3. Use substring or split

Related File I/O exercises

Practice all File I/O exercises · Run this idea in the Java compiler