BufferedReader implements Closeable. Try with resources calls close() himself. P/S. Solution generates correct output.