Lab 08 10 pts Week 3 Due Apr 17

Lab 08: Command Parser

Parsing structured text input with switch and String methods

switch-string startsWith substring trim toLowerCase
Clone from GitHub
3 checkpoints 5 autograder 2 timeliness
Prerequisites: lesson-1-7 lesson-1-13

Learning Objectives

After completing this lab, you will be able to:

  • Use a switch statement to dispatch behavior based on String input
  • Apply startsWith(), substring(), and trim() to extract data from text
  • Normalize user input with toLowerCase() before comparison
  • Handle unknown or malformed commands gracefully

What You’re Building

A simple text command parser that reads lines like !help, !roll, !flip, and !greet Alice from the console and dispatches each to the correct handler. This is the same pattern behind chatbot commands, CLI tools, and game consoles.

Your program reads commands in a loop until the user types !quit. Each recognized command prints a specific response. Unrecognized commands print an error message.


Concepts and Common Misconceptions

Concept What students get wrong
switch on Strings Forgetting that switch uses .equals() internally – the cases must match exactly.
startsWith Confusing it with contains(). startsWith only checks the beginning.
substring Off-by-one errors. substring(start) returns from start to end. substring(start, end) excludes end.
trim() Forgetting that trim() returns a new String. input.trim() alone does nothing unless you reassign: input = input.trim().
toLowerCase() Applying it to the switch variable but leaving cases in mixed case, or vice versa. Pick one convention.

Checkpoint 1: Simple Commands (1 pt)

Read a command string from the user. Use a switch statement to handle three commands:

  • !help – print "Available commands: !help, !roll, !flip, !greet <name>, !quit"
  • !roll – print "Rolling... 6" (hardcoded for now)
  • !flip – print "Heads" (hardcoded for now)

Normalize input with trim() and toLowerCase() before switching.

Debugging tip: If your switch never matches, print the command variable in quotes before the switch: System.out.println("[" + command + "]");. This reveals hidden whitespace or case mismatches.


Checkpoint 2: Parameterized Commands (1 pt)

Add support for !greet <name>. Since the command now has a parameter, you cannot switch on the full string. Use startsWith("!greet ") to detect the command, then extract the name with substring(7).

If the user types !greet Alice, print "Hello, Alice!". If they type !greet with no name (or only spaces after it), print "Usage: !greet <name>".

Structure your code so the startsWith check happens before the switch, and the switch handles the remaining fixed commands.

Debugging tip: "!greet".length() is 6, but you need index 7 to skip the space. Print command.substring(7) to verify you are extracting the name correctly.


Checkpoint 3: Input Validation (1 pt)

Add a default case to your switch that prints "Unknown command: <command>. Type !help for options." for any unrecognized input.

Wrap the entire parser in a while loop that continues reading commands until the user types !quit. After !quit, print "Goodbye!" and exit the loop.

Handle edge cases: empty input (just pressing Enter) should re-prompt without an error, and commands with extra whitespace like " !help " should still work.

Debugging tip: If your loop never exits, check that you are reading a new command inside the loop body, not reusing the same variable from before the loop.


How to Debug

  1. Print before you switch. Add System.out.println("CMD=[" + command + "]"); before the switch to see exactly what value you are matching against.
  2. Test one checkpoint at a time. Get !help working before adding !greet.
  3. Check your substring indices. If !greet Alice prints "ello, Alice!", your start index is off.
  4. Run the autograder after each checkpoint. Do not wait until the end.

Scoring Breakdown

Component Points
Checkpoint 1: Simple commands with switch 1
Checkpoint 2: Parameterized !greet command 1
Checkpoint 3: Validation + quit loop 1
Autograder correctness 5
Timeliness (on-time submission) 2
Total 10