Click here to Skip to main content
11,505,486 members (67,217 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 13-Feb-13 12:08pm
desho215
Edited 13-Feb-13 12:21pm
AspDotNetDev192.2K
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 13-Feb-13 18:13pm
   
i dont get it
AspDotNetDev at 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 at 13-Feb-13 18:20pm
   
a console application
AspDotNetDev at 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.
desho at 13-Feb-13 18:26pm
   
console.writeLine("plz enter a string");
string s = console.ReadLine();

but the user inter an integer instead.
AspDotNetDev at 13-Feb-13 20:08pm
   
That would not throw an exception. Also, a couple notes to more effectively use Code Project. 1) You can edit your question rather than replying to your question (this allows you to syntax highlight code snippets). 2) If you reply to my comment rather than commenting on your own question, I will get a notification and can respond to you faster.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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...

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.
  Permalink  
Comments
desho at 13-Feb-13 19:00pm
   
Thank u alot
Sergey Alexandrovich Kryukov at 13-Feb-13 21:20pm
   
I recommend you to accept it formally (green button).
—SA
valery possoz at 13-Feb-13 19:14pm
   
my pleasure.
Sergey Alexandrovich Kryukov at 13-Feb-13 20:29pm
   
Good, my 4. (Well, all that WriteLine is somewhat redundant)...
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use the regular expression to check whether the entered string literals is a number or contains any string.

sample:
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.
  Permalink  
Comments
desho at 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 at 13-Feb-13 18:55pm
   
Please include the namespace "using System.Text.RegularExpressions;"
desho at 13-Feb-13 19:00pm
   
Thanks alot
Sergey Alexandrovich Kryukov at 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 at 13-Feb-13 20:56pm
   
SA can you give an example string??
Sergey Alexandrovich Kryukov at 13-Feb-13 21:19pm
   
For example, UInt64.MaximumValue is 18446744073709551615, so "18446744073709551615" is valid, but, say" "18446744073709551785" is not. And how are you going to find the difference?
Only by UInt64.TryParse or UInt64.Parse with try-catch (by the way, did you see source code of them? different algorythms!). But if you do it anyway, no need for your Regex.
I did not vote, but this is not a solution...
—SA

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 552
1 F-ES Sitecore 380
2 Richard Deeming 270
3 CHill60 235
4 Richard MacCutchan 230
0 Sergey Alexandrovich Kryukov 1,067
1 Maciej Los 575
2 Peter Leow 540
3 F-ES Sitecore 525
4 DamithSL 470


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2015
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