Finite command processing
Finite command processing
A command is a small request to perform an operation. Define the command grammar before coding. For this application the accepted forms are add <positive ID> <one-word title>, complete <positive ID>, list, and quit. Commands are case-sensitive; titles are single tokens, a deliberate restriction for this first project. Leading, trailing and repeated spaces are ignored; tabs are not separators. The transcript is a finite array, so execution ends even without quit.
Separate tokenization, validation and execution. Tokenization splits a line into parts. Validation checks the number and meaning of those parts. Only then should the program modify task state. TryParse returns whether a conversion succeeded; check the value and the positivity rule separately.
using System;foreach (string line in new[] { " add 4 box ", " ", "list" }){ string[] parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); Console.WriteLine(parts.Length == 0 ? "Blank" : parts[0]);}Split returns an array. RemoveEmptyEntries prevents repeated spaces from producing empty tokens. Check Length before reading the first element.
Expected output (reasoned, not executed):
addBlanklistBuild on the first example
using System;using System.Globalization;foreach (string line in new[] { "add 12 box", "add -2 box", "add x box", "add 2" }){ var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); bool valid = parts.Length == 3 && parts[0] == "add" && int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out int id) && id > 0; Console.WriteLine(valid ? "Accepted" : "Invalid");}Short-circuit AND protects indexing and checks the conversion before using id. NumberStyles.None excludes signs and surrounding whitespace in the numeric token; overflow also fails. Positive leading-zero IDs are accepted. These examples use ordinary printable ASCII input and spaces. NumberStyles.None is not a complete lexical filter: integer parsing can ignore terminating NUL characters. Validate permitted characters and length separately for external input. Splitting on a literal space does not reject embedded tabs.
Expected output (reasoned, not executed):
AcceptedInvalidInvalidInvalidApply the idea to another case
using System;foreach (string line in new[] { "list", "oops", "quit", "list" }){ if (line == "quit") break; switch (line) { case "list": Console.WriteLine("List requested"); break; default: Console.WriteLine("Unknown command"); break; }}Console.WriteLine("Finished");The break in the if exits the foreach. The break in the switch exits the switch only. No command after quit runs.
Expected output (reasoned, not executed):
List requestedUnknown commandFinishedCommon mistakes and design choices
Do not read parts[1] before verifying token count. Do not accept list extra as list: extra tokens are errors in the declared grammar. Do not turn a command string into executable code. The finite transcript avoids indefinite blocking; an interactive ReadLine loop would also need explicit EOF handling and a termination policy.
Practice
Validate complete <ID> without changing any task. Test complete 8, complete 0, complete x, complete 8 extra and a blank line. Use the same token rules and invariant integer conversion.
Use the manual practice task and knowledge check to verify your understanding. All examples target C# 14 / .NET 10; execution and ByteMind renderer support remain unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Finite command processing — knowledge check
Apply the lesson concepts to the following scenarios.
Why check parts.Length first?
What is the first token of " add 4 box " after removing empty entries?
Is a successful TryParse sufficient for a task ID?
What happens to commands after quit in example 3?
Under the stated grammar, is add 3 blue box valid?