Java CLI Task Tracker project

3 weeks ago 30
ARTICLE AD BOX

I am building a Task Tracker CLI project, currently working on the CLI structure to accept user input. I have used a switch statement to receive input from args. The problem I have is that I have assigned args[0] to a variable called command. This is the condition in the switch statement, to determine which case will be used. I am not sure how to recognize the cases for strings that have spaces. For example, I have a "list" command and a "list done" command, but when I test it, the list done command is never read, because of the spaces. I have tried using if statements after the case statements, but I could not get anything to work, and it just seems like ugly code or bad practice to add if statements to those cases. I plan to create methods for each case in a separate file, and was thinking maybe that will help, but I would still need to find a way to get the switch statement to recognize the command.

public class TaskTracker { public static void main(String[] args) { // Check if command is more than one string if (args.length < 1) { System.out.println("Please enter a command such as: <command> <id> <description> <status>"); return; // Removes the error message on empty input (What I Learned) Stops the main method from // trying to access args[0] } // Store the command in a variable String command = args[0].toLowerCase(); // Handles case-sensitive situations from user input (What I Learned) // Switch statement switch (command) { case "add": // FIXME System.out.println("Task added successfully (ID: 1)"); break; case "update": System.out.println("TODO"); // FIXME break; case "delete": System.out.println("TODO"); // FIXME break; case "mark-in-progress": System.out.println("TODO"); // FIXME break; case "mark-done": System.out.println("TODO"); // FIXME break; case "list": System.out.println("TODO"); // FIXME break; case "list done": System.out.println("list"); /* FIXME String with spaces are not being read from input */ break; case "list todo": System.out.println("TODO"); // FIXME break; case "list in-progress": System.out.println("TODO"); // FIXME break; default: System.out.println("Please enter a valid command: <command> <id> <description> <status>"); } } }

Am I on the right path? Or should I come up with a new approach? I thought of using while or for loops, but they wouldn't make more sense than a switch statement in this situation.

Read Entire Article