Putting it together
- Java course
- Beginner
- Lesson 87/116
- 6 min
Write a CLI app with a menu loop.
Putting it together: command loop
In the last step we will combine all pieces into a command loop. Commands come as a String[]; each is either add:X, list, or done:N.
for (String cmd : commands) {
if (cmd.startsWith("add:")) {
list.addTask(cmd.substring(4));
} else if (cmd.equals("list")) {
list.listTasks();
} else if (cmd.startsWith("done:")) {
int idx = Integer.parseInt(cmd.substring(5));
list.completeTask(idx);
}
}With substring(4) we extract the title and with Integer.parseInt the index. This is the skeleton of a real menu loop; reading user input will come later.
Integrating the project
In this lesson we brought all methods (Task, TodoList, addTask, listTasks, completeTask) together. The command loop runs over an array that simulates user input.
In a real CLI app, commands are read from String[] args or Scanner. You will see Scanner in the next group; then you can wire this skeleton to live user input.
Now we ask you to write this loop: process commands in order, routing each to the right method.
Practice Write the command loop
Walk the commands array in main with for-each. For each command: if add:X call list.addTask(cmd.substring(4)), if list call list.listTasks(), if done:N call list.completeTask(Integer.parseInt(cmd.substring(5))).
Starter code
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
TodoList list = new TodoList();
String[] commands = {"add:Shopping", "add:Homework", "list", "done:0", "list"};
// Process commands in order: add:X, list, done:N
}
}
class Task {
String title;
boolean done;
Task(String title) {
this.title = title;
this.done = false;
}
public String toString() {
return (done ? "[x] " : "[ ] ") + title;
}
}
class TodoList {
ArrayList<Task> tasks = new ArrayList<>();
void addTask(String title) {
tasks.add(new Task(title));
}
void listTasks() {
for (Task t : tasks) {
System.out.println(t);
}
}
void completeTask(int index) {
tasks.get(index).done = true;
}
}Sample solution
for (String cmd : commands) {
if (cmd.startsWith("add:")) {
list.addTask(cmd.substring(4));
} else if (cmd.equals("list")) {
list.listTasks();
} else if (cmd.startsWith("done:")) {
int idx = Integer.parseInt(cmd.substring(5));
list.completeTask(idx);
}
}Expected output
[ ] Shopping [ ] Homework [x] Shopping [ ] Homework
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Fill in the blank: detect the command type
if (cmd.____("add:")) {
list.addTask(cmd.substring(4));
}
if (cmd.startsWith("add:")) {
list.addTask(cmd.substring(4));
}
Explanation: cmd.startsWith("add:") checks the prefix.
2 What does Integer.parseInt(cmd.substring(5)) do?
- Throws an error
- Removes the "done:" prefix
- Divides cmd by 5
- Extracts the number 0 from "done:0" Correct answer
Explanation: substring(5) gives "0", parseInt turns it into an int.
3 Match command to action
| Term | Meaning |
|---|---|
add:Shopping |
Add a new task |
list |
Print the tasks |
done:0 |
Complete the first task |
done:1 |
Complete the second task |