Trying to use ToUpper
and ToLower
validation in a C# Console Application to verify if a user defined string is inside a Dictionary with string and int elements inside it, printing out the elements with ConstainsKey
afterward. For example (simplified):
Dictionary<string, int> example = new Dictionary<string, int>();
//int for number of each type of element
int elements = 3;
for (int i = 0; i < elements; i++)
{
Console.WriteLine("What is the key?");
string stringElement = Console.ReadLine();
//Validation loop
while (string.IsNullOrWhileSpace(stringElement))
{
Console.WriteLine("error");
Console.WriteLine("What is the key?");
stringElement = Console.ReadLine();
}
//Ask user for value
Console.WriteLine("What is the value?");
string numElementString = Console.ReadLine();
while (string.IsNullOrWhiteSpace(intElementString) || (!(int.TryParse(numElementString, out numElement))) || numElement <= 0)
{
Console.WriteLine("error");
Console.WriteLine("What is the value?");
numElementString = Console.ReadLine();
}
example.Add(stringElement, numElement);
}
//What does user want to have printed
Console.WriteLine("What elements are you looking for? Please type in the key you need.");
//Catch key user answers
string userAnswer = Console.ReadLine();
//userAnswer validation look for IsNullOrWhiteSpace
while (string.IsNullOrWhiteSpace(userAnswer))
{
Console.WriteLine("error");
Console.WriteLine("What elements are you looking for? Please type in the key you need.");
userAnswer = Console.ReadLine();
I got that far, I'm just having an issue with using the ToUpper
and ToLower
expressions to have the console print out the specific <string, int>
variables that are inside the dictionary the user wants from the specified user input. Any push in the right direction will help... just want a simple solution for now since I'm still getting into the programming language.