Lesson 6.8 · Write it
Pick the warnings out of a log
A whole programPython or Java8 tests, 6 of them hidden
On a phone? Put the lines in order instead
A program writes a log file, log.txt, with one message per line. Write a program that prints only the warnings, then how many there were.
- A warning is a line whose first four characters are
WARN, in capital letters, whatever follows them:WARN disk full,WARNING lowandWARN:fanare all warnings. A line that hasWARNsomewhere later, or starts withwarnorWarn, is not a warning. - Print each warning exactly as it is in the file, in the order they appear.
- After the last line of the file, print
Warnings:followed by the number of warnings. If there are none, that isWarnings: 0. - The file may be empty. A line may be empty or shorter than four characters. The last line may or may not end with a line end. No line starts with a space.
Your program is run several times, each time in a folder that holds only the files shown for that run. Spaces at the end of a printed line are ignored.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 8 times: the 2 examples below, and 6 hidden ones that check the rules above. Only what it prints is checked. Each run starts in an empty folder that holds only the files shown for it.
two of the four lines start with WARN (rules 1 to 3)
You type
Nothing
It prints
WARN disk 91% full WARN battery low Warnings: 2
Files in its folder
log.txt INFO started WARN disk 91% full INFO saved 3 files WARN battery low
one warning, on the last line (rules 2 and 3)
You type
Nothing
It prints
WARN low ink Warnings: 1
Files in its folder
log.txt INFO all fine WARN low ink