C
C#8mo ago
pikau

Accessing individual words in user input (Console.Readline())

I am looking to access specific words that the user inputs. For example, i have to first assess whether the first word inputted is a valid command (e.g. "add", "help" "exit" etc.) and then see if the word following that is a valid constraint for that command. How might i go about this? Here is my current code:
namespace A2
{
// Define a section to hold all the info that prompts the user to provide responses upon first opening the project.
public class UserInput
{
static void Main()
{
// First we must print a statement that can be anything to welcome the user to the macchine thing
// Write out custom message to the UI
Console.WriteLine($"Welcome to the Tom's Obstacle Avoidance System!");
// Write out the valid commands
Console.WriteLine($"Valid commands are:");
Commands.ListCommands();
// List other options
Console.WriteLine($"help: displays this help message");
Console.WriteLine($"exit: closes this program");

// While loop that keeps running while true (while the use hasnt yet entered exit) that checks
// the validity/input of the user.
while (true)
{
// Prompt the user to enter a command.
Console.WriteLine($"Enter command:");
// Read the users input
string UserInput = Console.ReadLine();
// If the user says exit, then we terminate the program
// Check if it is exit first to avoid unnessary iterating of the valid commands list if it is "exit"
if (UserInput == "exit")
{
// Terminate
break;
}

// If the user enters something else other than exit, then we can check if it is a valid command
if (!Commands.ValidCommands.Contains(UserInput))
{
// Inform the user that their input is invalid, returning their input back to them.
Console.WriteLine("Invalid option: {0}", UserInput);
// Prompting the help message
Console.WriteLine($"Type 'help' to see a list of commands.");
}

if (UserInput == "help")
{
Console.WriteLine($"Valid commands are:");
Commands.ListCommands();
Console.WriteLine($"help: displays this help message");
Console.WriteLine($"exit: closes this program");
}

if (UserInput == "add")
{
switch (UserInput)
{
case "gaurd":
Console.WriteLine($"Chosen Gaurd");
break;
case "fence":
Console.WriteLine($"Chosen Fence");
break;
case "sensor":
Console.WriteLine($"Chosen Sensor");
break;
case "camera":
Console.WriteLine($"Chosen Camera");
break;
}
}
else
Console.WriteLine($"You need to specify an obstacle type.");
if (!Obstacles.ValidObstacles.Contains(UserInput))
// Inform the user that their input is invalid, returning their input back to them.
Console.WriteLine("Invalid option: {0}", UserInput);
}

}
}
}
namespace A2
{
// Define a section to hold all the info that prompts the user to provide responses upon first opening the project.
public class UserInput
{
static void Main()
{
// First we must print a statement that can be anything to welcome the user to the macchine thing
// Write out custom message to the UI
Console.WriteLine($"Welcome to the Tom's Obstacle Avoidance System!");
// Write out the valid commands
Console.WriteLine($"Valid commands are:");
Commands.ListCommands();
// List other options
Console.WriteLine($"help: displays this help message");
Console.WriteLine($"exit: closes this program");

// While loop that keeps running while true (while the use hasnt yet entered exit) that checks
// the validity/input of the user.
while (true)
{
// Prompt the user to enter a command.
Console.WriteLine($"Enter command:");
// Read the users input
string UserInput = Console.ReadLine();
// If the user says exit, then we terminate the program
// Check if it is exit first to avoid unnessary iterating of the valid commands list if it is "exit"
if (UserInput == "exit")
{
// Terminate
break;
}

// If the user enters something else other than exit, then we can check if it is a valid command
if (!Commands.ValidCommands.Contains(UserInput))
{
// Inform the user that their input is invalid, returning their input back to them.
Console.WriteLine("Invalid option: {0}", UserInput);
// Prompting the help message
Console.WriteLine($"Type 'help' to see a list of commands.");
}

if (UserInput == "help")
{
Console.WriteLine($"Valid commands are:");
Commands.ListCommands();
Console.WriteLine($"help: displays this help message");
Console.WriteLine($"exit: closes this program");
}

if (UserInput == "add")
{
switch (UserInput)
{
case "gaurd":
Console.WriteLine($"Chosen Gaurd");
break;
case "fence":
Console.WriteLine($"Chosen Fence");
break;
case "sensor":
Console.WriteLine($"Chosen Sensor");
break;
case "camera":
Console.WriteLine($"Chosen Camera");
break;
}
}
else
Console.WriteLine($"You need to specify an obstacle type.");
if (!Obstacles.ValidObstacles.Contains(UserInput))
// Inform the user that their input is invalid, returning their input back to them.
Console.WriteLine("Invalid option: {0}", UserInput);
}

}
}
}
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server