A To-Do List is a practical project for Java beginners. It demonstrates user input, collections, menu-driven logic, and the basics of organizing data. Let’s break down the full process with easy explanations and a complete code example.
What Will This To-Do List App Do?
- Allow users to add tasks.
- Show the current list of tasks.
- Remove tasks by number.
- Exit the app via menu.
All these actions will be performed in the terminal/console, using only core Java (no GUIs or external libraries).
Step 1: Required Java Concepts
This mini-project will help you practice:
- Using
ArrayList
to store tasks dynamically. - Writing loops and switch/case (menu).
- Accepting and validating user input with Scanner.
- Writing and calling methods for organization.
Step 2: Import Required Classes
java
import java.util.Scanner;
import java.util.ArrayList;
Step 3: Define the Main Program Structure
You’ll need:
- An
ArrayList<String>
to store your tasks. - A loop to repeatedly show the menu.
- Methods for each operation (add, remove, show).
java
public class ToDoListApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> tasks = new ArrayList<>();
int choice;
do {
System.out.println("\n--- To-Do List Menu ---");
System.out.println("1. Add Task");
System.out.println("2. View Tasks");
System.out.println("3. Remove Task");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Clear the newline
switch (choice) {
case 1:
System.out.print("Enter task description: ");
String task = scanner.nextLine();
tasks.add(task);
System.out.println("Task added!");
break;
case 2:
System.out.println("Your tasks:");
if (tasks.isEmpty()) {
System.out.println("No tasks yet.");
} else {
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i+1) + ". " + tasks.get(i));
}
}
break;
case 3:
if (tasks.isEmpty()) {
System.out.println("No tasks to remove!");
} else {
System.out.println("Enter the task number to remove:");
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i+1) + ". " + tasks.get(i));
}
int removeIndex = scanner.nextInt();
scanner.nextLine(); // Clear newline
if (removeIndex > 0 && removeIndex <= tasks.size()) {
tasks.remove(removeIndex - 1);
System.out.println("Task removed.");
} else {
System.out.println("Invalid task number!");
}
}
break;
case 4:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice! Please enter 1-4.");
}
} while (choice != 4);
scanner.close();
}
}
Explanation of Key Parts
1. ArrayList tasks
- Stores the description of each to-do task.
- ArrayList is used because tasks can be added/removed as needed.
2. The Menu Loop (do-while)
- Keeps running until the user selects "Exit".
- Each case in switch shows the related operation.
-
scanner.nextLine()
is used to read task descriptions (and clear newlines).
3. Adding and Viewing Tasks
- Adding: Prompt user, append to tasks list.
- Viewing: Loop through tasks, display with numbering.
4. Removing Tasks
- Show current tasks with numbers.
- Ask for the number, remove the corresponding task (after checking it’s valid).
5. Exiting
- Case 4 in the switch ends the loop and says goodbye.
How to Run the To-Do List
1.Copy the complete code into a file named ToDoListApp.java
.
2.Open a terminal in the file’s folder.
3.Compile:
text
javac ToDoListApp.java
4.Run:
text
java ToDoListApp
5.Use menu options to manage your to-do list!
Possible Expansions
Once you complete the basic version, try to:
- Save/load tasks from a file (using Java I/O).
- Add task priorities or deadlines.
- Mark tasks as completed/incomplete.
- Add input validation for empty or blank tasks.
Why This Project Helps Beginners
- Reinforces loop, collection, and input basics.
- Shows how to build a usable, interactive tool.
- Teaches menu-driven programming — a core skill.
- Easily extendable as your skills grow.
With a simple To-Do List app, you practice logical thinking, Java syntax, and clean structure. Finish this project, then tweak and expand it to solidify your Java foundations!
Check out the YouTube Playlist for great java developer content for basic to advanced topics.
Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ... : CodenCloud
Top comments (0)