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);
}
}
}
}
hours
price
sauna
quit> Open every day from 7 to 21
> A swim costs 6 euros
> Unknown command: sauna
> Bye!while (true)repeats until abreakstops it. Each round reads one command and answers it.- The
quitcheck comes right after reading.breakends the loop, and since nothing comes after the loop, the program ends too. - Each command has its own
iforelse if. The lastelseanswers everything else, so a typo likehourgets 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 withequals.
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
>withprint. 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.