Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET C#4.0
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 ?
 
 

 
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 17-Mar-13 1:43am
Edited 17-Mar-13 1:44am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The problem is that when the user types a key, you try to parse it to an int:
            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:
            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:
            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";
                        }
                    }
                ...
                }
  Permalink  
Comments
Silent Guardian at 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 at 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 at 18-Mar-13 12:46pm
   
oops sorry, I was thinking of Console.ReadKey().KeyChar
and yes it needs a casting.
OriginalGriff at 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)

  Print Answers RSS
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 214
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 163
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 17 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100