Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do we handle an exception that occurs when you ask the user to enter an integer, but they instead enter a string which is not a valid integer?
Posted
Updated 13-Feb-13 12:21pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Feb-13 18:11pm    
To start with, the user always enters a string... :-)
All exceptions are handlers in similar ways; not clear what's your problem. Just try to develop this code, and you will see.
You don't have to throw exception, as you can use int.TryParse.
—SA
desho 13-Feb-13 18:13pm    
i dont get it
AspDotNetDev 13-Feb-13 18:18pm    
This is called validation, and there are many ways to handle it. First off, are you making a Windows Forms, ASP.NET Web Forms, ASP.NET MVC, WPF, Silverlight, a console application, or something else?
desho 13-Feb-13 18:20pm    
a console application
AspDotNetDev 13-Feb-13 18:22pm    
Please update your question with the code that you are using. Make the code sample as small as possible, but fully demonstrate the problem. This will help us give you a quick and complete answer.

Hello

The simplest solution is to try to convert the string to an integer, if it works it is an integer if it does not, it is not...

C#
Console.WriteLine("plz enter a string");
string s = Console.ReadLine();

int result;
if (int.TryParse(s, out result))
{
    Console.WriteLine("you have entered the number: " + s);
}
else
{
    Console.WriteLine(s + " is not a number");
}



Valery.
 
Share this answer
 
Comments
desho 13-Feb-13 19:00pm    
Thank u alot
Sergey Alexandrovich Kryukov 13-Feb-13 21:20pm    
I recommend you to accept it formally (green button).
—SA
Valery Possoz 13-Feb-13 19:14pm    
my pleasure.
Sergey Alexandrovich Kryukov 13-Feb-13 20:29pm    
Good, my 4. (Well, all that WriteLine is somewhat redundant)...
—SA
You can use the regular expression to check whether the entered string literals is a number or contains any string.

sample:
C#
string s = "This is string";

string s2 = "10";
string s3 = "10s";

Regex regex = new Regex(@"^\d+$");
bool check = regex.IsMatch(s); // false
check = regex.IsMatch(s2); // true
check = regex.IsMatch(s3); //false


check the return value of IsMatch and prompt the user with error.
 
Share this answer
 
Comments
desho 13-Feb-13 18:53pm    
Error 1 The type or namespace name 'Regex' could not be found (are you missing a using directive or an assembly reference?)
Jibesh 13-Feb-13 18:55pm    
Please include the namespace "using System.Text.RegularExpressions;"
desho 13-Feb-13 19:00pm    
Thanks alot
Sergey Alexandrovich Kryukov 13-Feb-13 20:27pm    
Sorry, I think this is not an effective method. For example, the string may represent some number bigger them *.MaximumValue.
The only reliable and effective method is TryParse.
—SA
Jibesh 13-Feb-13 20:56pm    
SA can you give an example string??

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