Remembering between commands
The info desk answers every command the same way, whatever came before it. Most programs have to remember something from one command to the next: a count, a total, the names typed so far. Variables do that, with one rule: a variable that must last from command to command is created before the loop. A variable created inside the loop's braces is made new in every round and forgotten at the end of it.
A musician logs practice sessions. log is followed by a line with the minutes practiced, total adds up all the sessions, and longest finds the longest one:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> sessions = new ArrayList<>();
while (true) {
System.out.print("> ");
String command = scanner.nextLine();
if (command.equals("quit")) {
System.out.println("Keep practicing!");
break;
}
if (command.equals("log")) {
int minutes = Integer.valueOf(scanner.nextLine());
sessions.add(minutes);
System.out.println("Logged " + minutes + " minutes");
} else if (command.equals("total")) {
int total = 0;
for (int minutes : sessions) {
total = total + minutes;
}
System.out.println("Total: " + total + " minutes");
} else if (command.equals("longest")) {
int longest = 0;
for (int minutes : sessions) {
if (minutes > longest) {
longest = minutes;
}
}
System.out.println("Longest: " + longest + " minutes");
} else {
System.out.println("Unknown command: " + command);
}
}
}
}
log
25
log
40
total
log
15
longest
quit> Logged 25 minutes
> Logged 40 minutes
> Total: 65 minutes
> Logged 15 minutes
> Longest: 40 minutes
> Keep practicing!sessionsis created before the loop, so every round adds to the same list, andtotalsees every session logged so far. Created inside the loop, it would start empty in every round, andtotalwould always say 0 minutes.logreads the minutes from the line after the command, as the previous step described:Integer.valueOf(scanner.nextLine()).totalandlongestare created inside their own branches. They're only needed while that one answer is worked out, so they're worked out again from the list each time.
Small solutions, put together
The program looks long, but each part of it is something you've written before:
- Read commands until
quit, and answer unknown ones: the loop of the previous step. - Remember each session: add it to a list.
- Add up the values of a list.
- Find the largest value of a list.
That's a good way into any bigger program: list the small problems it's made of, solve them one at a time, and try each one before going on. Here you could first write the loop with only quit and the unknown-command answer, then log and total, and longest last. Each piece stays small enough to check at a glance.
What to remember
Remember what later commands need. A list of sessions is one way to do it. But total and longest could also be answered with two int variables, updated at every log, without keeping the sessions at all. A list is needed when a command needs every value again, for example to print all the sessions. Step 5 comes back to this choice.