Java Arena
Step 1 of 7

A loop that reads commands

Many programs work like a conversation: the user types a command, the program answers, and then it waits for the next command, until the user types the one that ends it. The shopping list at the end of module 14 was one of them. This module is about writing such programs so that they stay easy to read and to change as they grow. It starts with everything in main, and then splits the program into a class that does the work and a class that talks with the user.

Here's the starting point, an info desk at a swimming pool. It knows two commands, and quit:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("> ");
            String command = scanner.nextLine();
            if (command.equals("quit")) {
                System.out.println("Bye!");
                break;
            }
            if (command.equals("hours")) {
                System.out.println("Open every day from 7 to 21");
            } else if (command.equals("price")) {
                System.out.println("A swim costs 6 euros");
            } else {
                System.out.println("Unknown command: " + command);
            }
        }
    }
}
Input
hours
price
sauna
quit
Output
> Open every day from 7 to 21
> A swim costs 6 euros
> Unknown command: sauna
> Bye!
  • while (true) repeats until a break stops it. Each round reads one command and answers it.
  • The quit check comes right after reading. break ends the loop, and since nothing comes after the loop, the program ends too.
  • Each command has its own if or else if. The last else answers everything else, so a typo like hour gets a message. Without it, the program would say nothing, and the user couldn't tell whether anything happened.
  • The command is read once in each round, into command, and every branch compares that same variable with equals.

The prompt, and what the outputs show

System.out.print("> ") prints a prompt: a sign that the program is waiting for a command. It's printed with print, not println, so what the user types goes on the same line. In a terminal, the conversation above looks like this:

> hours
Open every day from 7 to 21
> price
A swim costs 6 euros
> sauna
Unknown command: sauna
> quit
Bye!

As in module 2, the checks on this site give the program all of its input at the start, and the output doesn't show what was typed. So each answer comes right after its > , as in the output block above. Every program in this module works this way:

  • Before reading a command, it prints > with print. Each > in an output is one command read, and the rest of that line is the program's answer.
  • A command that needs a value, such as a name or a number, reads it from the next line, without a prompt of its own.

Your task

Challenge 1 of 3

A display at a tram stop answers commands: next prints Tram 4 in 6 minutes, help prints Commands: next, help, quit, and quit prints Goodbye and ends the program. Anything else prints Unknown command: and the command. Fill in the blanks. For the input shown, the program prints:

> Tram 4 in 6 minutes
> Commands: next, help, quit
> Unknown command: bus
> Goodbye

Challenge 2

A virtual cat answers commands. Write the command loop. Before reading each command, print the prompt > with print. The commands:

  • feed prints The cat eats and purrs
  • pet prints The cat closes its eyes
  • sleep prints The cat curls up. Goodnight! and ends the program
  • anything else prints The cat ignores ", the command and a closing ", like The cat ignores "bath"

For the input shown, the program prints:

> The cat eats and purrs
> The cat ignores "bath"
> The cat closes its eyes
> The cat curls up. Goodnight!

Challenge 3

This program should answer every command, but for the input shown it prints only > Unknown command and > Disconnected. With other input it even crashes. Find out why, and fix it, so that it prints:

> pong
> Server 2.4
> pong
> Disconnected