Java Arena
Step 6 of 9

Searching a file

Many questions about a file are answered by going through its lines once and checking each one. How many lines mention something? Is a certain line there at all? This program counts and prints the lines of a log that contain the word ERROR:

The file log.txt
08:00 start
08:15 ERROR disk almost full
09:30 backup done
10:05 ERROR connection lost
10:06 connection back
import java.nio.file.Paths;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int errors = 0;
        try (Scanner scanner = new Scanner(Paths.get("log.txt"))) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (line.contains("ERROR")) {
                    System.out.println(line);
                    errors++;
                }
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println("Errors: " + errors);
    }
}
Output
08:15 ERROR disk almost full
10:05 ERROR connection lost
Errors: 2

Any String method can do the checking: contains for a part of the line, startsWith for its beginning, equals for the whole line, equalsIgnoreCase when capitals shouldn't matter.

Is a line there?

To find out whether something is in the file at all, keep a boolean that starts as false and becomes true when a line matches. The answer is known only after the loop, once every line has been checked. Here the user types a discount code, and the program looks for it in the file of valid codes:

The file codes.txt
WELCOME5
SPRING10
FRIEND20
import java.nio.file.Paths;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String code = scanner.nextLine();
        boolean found = false;
        try (Scanner fileScanner = new Scanner(Paths.get("codes.txt"))) {
            while (fileScanner.hasNextLine()) {
                if (fileScanner.nextLine().equals(code)) {
                    found = true;
                }
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
        if (found) {
            System.out.println("Code accepted");
        } else {
            System.out.println("Unknown code");
        }
    }
}
Input
SPRING10
Output
Code accepted
  • equals compares whole lines. With contains, the code RING1 would be accepted too, since it's inside SPRING10.
  • The answer is printed after the loop. Printed inside it, there would be an answer for every line of the file.

Your task

Challenge 1 of 3

The file reviews.txt holds a restaurant's reviews, one per line. The program reads a word and counts the reviews that mention it. Fill in the blanks. For the input pizza and the file shown below, it prints:

Reviews that mention pizza: 3

Challenge 2

Read a word, and print the number of the first line of diary.txt that contains it, as First on line and the number. If no line contains it, print Not in the file. For the input apple and the file shown below, the program prints:

First on line 2

Challenge 3

What does this program print? Compare equals and contains on every line.