Taran Taran - 2 months ago 23
C# Question

Limiting c# user interface to one input

I have written an application to determines whether the character enter is Uppercase, Lowercase or numerical. However, I was wondering how I could make the application limit the amount of characters a user can input to just one?

class Program
{
static void Main(string[] args)
{
try
{
Console.Write("Enter a character: ");
char c = (char)Console.Read();
if (Char.IsLetter(c))
{
if (Char.IsLower(c))
{
Console.WriteLine("The character is lowercase");
Console.Write("Press any key to exit...");
Console.ReadKey();
}
else
{
Console.WriteLine("The character is uppercase");
Console.Write("Press any key to exit...");
Console.ReadKey();
}

}

else
{
Console.WriteLine("Not an alphabetic character");
Console.Write("Press any key to exit...");
Console.ReadKey();
}
} // Try

catch {

}

}
}

Answer

Use

Console.Write("Enter a character: ");
                char c = (char)Console.ReadKey().KeyChar;
                if (Char.IsLetter(c))

This will limit the entry to just one character