THINK FIRST·CODE LATER

← All labs

Config loader: a custom checked exception

Problem

A server reads its settings as key = value lines (empty lines and lines starting with # are ignored). Valid keys are port, threads and timeout, with defaults 8080, 4 and 30.

  1. Write a checked exception ConfigException extends Exception that stores the line number; its message is line 3: ….
  2. Write static int[] load(Scanner in) throws ConfigException that throws it for: a missing =, an unknown key, a value that is not a whole number (catch the NumberFormatException and translate it: timeout must be a whole number, got 'ten') and a value ≤ 0.
  3. main calls load, prints the three settings and Configuration OK, or Configuration error, line 2: unknown key 'host'; a finally block closes the Scanner and prints (input closed).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l16-3-data-1.txt

# server
port = 9000

threads=8
What your program must print for l16-3-data-1.txt
port = 9000
threads = 8
timeout = 30
Configuration OK
(input closed)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l16-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content l16-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.