Introduction
A to-do list application helps users keep track of tasks they need to complete. This simple to-do list app in Java allows users to add, remove, and view their tasks. The application is console-based and utilizes basic concepts of Java such as arrays, loops, and input handling.
Objective
The objective of this project is to create a simple, user-friendly console-based to-do list application in Java. The application will:
- Allow users to add tasks to their to-do list.
- Allow users to view the list of tasks.
- Allow users to remove tasks from the list.
- Loop until the user decides to exit the program.
Java Code for the To-Do List Application
import java.util.ArrayList; import java.util.Scanner; public class TodoListApp { // List to store tasks private static ArrayList tasks = new ArrayList<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int choice; while (true) { System.out.println("\n===== To-Do List Application ====="); System.out.println("1. Add a Task"); System.out.println("2. View Tasks"); System.out.println("3. Remove a Task"); System.out.println("4. Exit"); System.out.print("Please enter your choice (1-4): "); choice = scanner.nextInt(); scanner.nextLine(); // Consume newline character switch (choice) { case 1: System.out.print("Enter the task to add: "); String taskToAdd = scanner.nextLine(); addTask(taskToAdd); break; case 2: viewTasks(); break; case 3: System.out.print("Enter the task number to remove: "); int taskToRemove = scanner.nextInt(); removeTask(taskToRemove); break; case 4: System.out.println("Exiting the application. Goodbye!"); scanner.close(); return; default: System.out.println("Invalid choice! Please try again."); } } } // Method to add a task private static void addTask(String task) { tasks.add(task); System.out.println("Task added successfully!"); } // Method to view all tasks private static void viewTasks() { if (tasks.isEmpty()) { System.out.println("No tasks available."); } else { System.out.println("\nYour To-Do List:"); for (int i = 0; i < tasks.size(); i++) { System.out.println((i + 1) + ". " + tasks.get(i)); } } } // Method to remove a task by index private static void removeTask(int index) { if (index >= 1 && index <= tasks.size()) { tasks.remove(index - 1); System.out.println("Task removed successfully!"); } else { System.out.println("Invalid task number! Please try again."); } } }
Explanation of the Program
This simple To-Do List application is built using the Java programming language. Let’s break down its structure:
1. Importing Required Classes
The program imports two classes:
ArrayList
: Used to store the tasks dynamically, as the size of the to-do list can vary.Scanner
: Used to take user input from the console.
2. Core Logic
The program continuously runs in a loop, presenting a menu with the following options:
- Option 1: Add a task to the list.
- Option 2: View the current list of tasks.
- Option 3: Remove a specific task from the list based on its index.
- Option 4: Exit the application.
3. Methods in the Program
The program contains the following methods:
addTask(String task)
: Adds the task to the list.viewTasks()
: Displays all tasks in the to-do list with their corresponding numbers.removeTask(int index)
: Removes the task at the specified index from the list.
4. User Interaction
The user interacts with the application via the console, entering numbers to select options and provide task information. The application will continue running until the user chooses the option to exit.
5. Error Handling
There is basic error handling for invalid task numbers when removing tasks, ensuring that the user provides valid input. If the user enters a number outside the valid range, the program will prompt them to try again.
How to Run the Program
To run the To-Do List application, follow these steps:
-
- Install Java on your machine if it’s not already installed. You can download it from the official Java website: Java Downloads.
- Open a text editor or IDE (like IntelliJ IDEA, Eclipse, or Visual Studio Code) and create a new Java file named
TodoListApp.java
. - Copy and paste the code provided above into the Java file.
- Open a terminal or command prompt and navigate to the directory where your
TodoListApp.java
file is located. - Compile the Java program by running the following command:
javac TodoListApp.java
-
- Run the compiled Java program with the following command:
java TodoListApp
- The application will now run in your terminal, and you can interact with the to-do list as described.