import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // Create a list to store tasks ArrayList tasks = new ArrayList();
// Create a Scanner object for user input Scanner scanner = new Scanner(System.in);
while (true) { // Display menu options System.out.println("1. Add Task"); System.out.println("2. View Tasks"); System.out.println("3. Remove Task"); System.out.println("4. Exit");
// Get user choice System.out.print("Choose an option: "); //You declare a variable named choice of type String. This variable will store the input read from the user. The scanner.nextLine() method reads an entire line of text entered by the user String choice = scanner.nextLine();
// Handle user choice switch (choice) { case "1": // Add a task System.out.print("Enter a task: "); String taskToAdd = scanner.nextLine(); tasks.add(taskToAdd); System.out.println("Task added."); break;
case "2": // View tasks System.out.println("Tasks: " + tasks); break;
case "3": // Remove a task System.out.print("Enter task to remove: "); String taskToRemove = scanner.nextLine(); if (tasks.contains(taskToRemove)) { tasks.remove(taskToRemove); System.out.println("Task removed."); } else { System.out.println("Task not found."); } break;
case "4": // Exit the program System.out.println("Exiting program. Goodbye!"); scanner.close(); return; // Exit the main method
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Create a list to store tasks
ArrayList tasks = new ArrayList();
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
while (true) {
// Display menu options
System.out.println("1. Add Task");
System.out.println("2. View Tasks");
System.out.println("3. Remove Task");
System.out.println("4. Exit");
// Get user choice
System.out.print("Choose an option: ");
//You declare a variable named choice of type String. This variable will store the input read from the user. The scanner.nextLine() method reads an entire line of text entered by the user
String choice = scanner.nextLine();
// Handle user choice
switch (choice) {
case "1":
// Add a task
System.out.print("Enter a task: ");
String taskToAdd = scanner.nextLine();
tasks.add(taskToAdd);
System.out.println("Task added.");
break;
case "2":
// View tasks
System.out.println("Tasks: " + tasks);
break;
case "3":
// Remove a task
System.out.print("Enter task to remove: ");
String taskToRemove = scanner.nextLine();
if (tasks.contains(taskToRemove)) {
tasks.remove(taskToRemove);
System.out.println("Task removed.");
} else {
System.out.println("Task not found.");
}
break;
case "4":
// Exit the program
System.out.println("Exiting program. Goodbye!");
scanner.close();
return; // Exit the main method
default:
// Handle invalid input
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
}