Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the user to press a key from keyboard and want the computer to tell if the key is a alphabet, symbol or a number. But some error is there in the code. Can you point out ?

C#
Console.WriteLine("type a character");
            int character = int.Parse(Console.ReadLine());
            string type = " ";

            if ((character>=48) && (character<=57))
            {
                type = "integer";
            }

            else if ((character >= 65) && (character <= 90))
            {
                type = "uppercase alphabet";

            }


            else if ((character >= 97) && (character <= 122))
            {
                type = "lowecase character";

            }

            else
            {
                type = "symbol";
            }
           
           
            
            Console.WriteLine("the type of character that you have entered is " +type);
            Console.ReadLine();
Posted
Updated 17-Mar-13 0:44am
v2

1 solution

The problem is that when the user types a key, you try to parse it to an int:
C#
int character = int.Parse(Console.ReadLine());
Which means convert from a string representation of a number "1234" to an integer version that can be handle mathematically.

So if the user types an "8" for example, int.Parse will return the value 8 - which is less than 48, so it will fail all your tests.

Instead, use the character directly:
C#
string userInput = Console.ReadLine();
foreach (char c in userInput)
    {
    string type = "unknown";
    if (c >= '0' && c <= '9')
        {
        type = "integer";
        }
    else if ...

Or better, use the built in functions:
C#
string userInput = Console.ReadLine();
foreach (char c in userInput)
    {
    string type = "symbol";
    if (char.IsDigit(c))
        {
        type = "integer";
        }
    else if (char.IsLetter(c))
        {
        if (char.IsLower(c))
            {
            type = "lowercase character";
            }
        else
            {
            type = "uppercase character";
            }
        }
    ...
    }
 
Share this answer
 
Comments
Silent Guardian 18-Mar-13 11:55am    
instead of Console.ReadLine() y not use Console.Read()?
and directly parse it.add the whole code in a while loop.
because Console.ReadLine() would wait for the return key.
OriginalGriff 18-Mar-13 12:30pm    
Um.
So does Console.Read - it doesn't return with the first key pressed until the user presses the return key...
And it returns an int, so you need to cast it to do "proper" character comparisons.
Silent Guardian 18-Mar-13 12:46pm    
oops sorry, I was thinking of Console.ReadKey().KeyChar
and yes it needs a casting.
OriginalGriff 18-Mar-13 12:49pm    
You know you're relying on intellisense too much when you assume it will work in a textbox! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900