c# filtering user input

Thread Starter

Dritech

Joined Sep 21, 2011
907
Hi all,

I am doing a small console program in C# where I am asking the user to input his name, and telephone number.
Now, I wish to make the program to recognize if the phone number is all in integer form and the name in character form.
Note that the data type is of string for both the name and the phone number.

I did some research and found that TryParse can be used to check for integer input, but how can I ask the user to rewrite the name?

Below is my current code:

Console.Write("Enter Name: ");
name = Console.ReadLine();
Console.Write("Phone number: ");
phone = Console.ReadLine();


Any help would be highly appreciated and sorry for my poor English.
 

vpoko

Joined Jan 5, 2012
267
You could put your code into a do/while loop.

For example:
Code:
do
{
Console.Write("Enter Name: ");
name = Console.ReadLine(); 
}
while (!isValid(name));
isValid() is a function you'd define to validate the name.
 

Thread Starter

Dritech

Joined Sep 21, 2011
907
Thanks for the reply. Anther quick question, is it possible to increment a list counter as below?

My list:
List<example>exampleList = new List<example>();

This is my attempt to increment the counter:
exampleList.Count++;

When doing this, I am getting the error telling me ".Count has no setter". Note that the list is declared in the same class.
What is causing this please?
 

sirch2

Joined Jan 21, 2013
1,071
List.Count tells you how many items are in the list, as the the error message indicates, you cannot set it.
 

Thread Starter

Dritech

Joined Sep 21, 2011
907
List.Count tells you how many items are in the list, as the the error message indicates, you cannot set it.
Hi, the problem is that I am using list.Count as an identification for every person. When I delete a person from the list, the last person number used (from the count) is being repeated.
To better explain this, assume I have the list below:

person no: 0
person name: Peter
person no: 1
person name: Luke
person no: 2
person name: Pat

When I remove Luke from the list and add another person, the person number is being the same as the last one (i.e. 2). To prevent this I was going to increment the counter with every delete.
Is there another method please?
 

vpoko

Joined Jan 5, 2012
267
You need to create your own counter, which should be a static (meaning shared) member of your class. You'll want to call it something other than count. Then you can define a constructor that will automatically assign that current number to the newly-created member, and then increment the counter.
 

sirch2

Joined Jan 21, 2013
1,071
I would create a Person class with firstname, lastname, etc. and an Id property to store the unique number. When creating a person you can then assign an Id (number) and add it to a List<Person>. You then don't have to track anything, assuming you never insert into the middle of the list, only add to then end then

newPersion.Id = personList[personList.Count-1].Id+1;

Although vpoko's suggestion is equally valid
 

tshuck

Joined Oct 18, 2012
3,534
I would create a Person class with firstname, lastname, etc. and an Id property to store the unique number. When creating a person you can then assign an Id (number) and add it to a List<Person>. You then don't have to track anything, assuming you never insert into the middle of the list, only add to then end then

newPersion.Id = personList[personList.Count-1].Id+1;

Although vpoko's suggestion is equally valid
By that time, you might as well use a dictionary...

Are you using the Add method to add to the list?
 

sirch2

Joined Jan 21, 2013
1,071
By that time, you might as well use a dictionary...

Are you using the Add method to add to the list?
Not sure how a dictionary would help, dictionary is a hashmap and so good at finding entries quickly by a specific index but does nothing to enforce keeping the person number with the person.
 

tshuck

Joined Oct 18, 2012
3,534
Not sure how a dictionary would help, dictionary is a hashmap and so good at finding entries quickly by a specific index but does nothing to enforce keeping the person number with the person.
It would be an approach closer to what the OP is doing already.

No, it doesn't enforce a person-specific numbering scheme, but the OP hasn't said anything that would preclude this approach.
 

Thread Starter

Dritech

Joined Sep 21, 2011
907
Thanks for the replies. One last question. I used TryParse to check that the phone number is all of type integers:


int test;
if (Int.TryParse(value, out test) == true)
{
number = value;
}
else
{
Console.Write("incorrect number");ì
}

Can I use a similar method to check that the name inputted is all of type char?
 

djsfantasi

Joined Apr 11, 2010
9,237
All type char? So %#^~\|}{][><=@&$)( etc... are valid in the name? Do you mean alphabetic? Plus likely a period for Mr. and Jr. and the like.

I did some research and found a description using Regex in c#. If you are familiar with or can become so, regex is a powerful tool. You can specify a template and validate almost anything. To check for all alpha, try this:

Regex.IsMatch(input, @"^[a-zA-Z]+$");

Regex is in the System.Text.RegularExpressions namespace.
 

djsfantasi

Joined Apr 11, 2010
9,237
For example, to validate a telephone number that matches the form 555-555-1212, one would use the Regex pattern which follows:

^[1-9][0-9]{2}[-][1-9][0-9]{2}[-][0-9]{4}$
The area code must start ("^") with a digit other than zero ("[1-9]"), followed by two digits ("[0-9]{2}") and be followed by a dash ("[-]")

That is followed by the exchange which must start with a digit other than zero, followed by two digits and be followed by a dash. I didn't include the Regex code, because you should be able to interpret it from the validation of the area code.

The last four digits can be any digit from 0-9, exactly 4 times. At that point, the string must end ("$")
 

djsfantasi

Joined Apr 11, 2010
9,237
Can I use Regex.IsMatch to only accept float data type? (example for prices)
Yes. Start at the first character ("^"), match a variable number of digits ("[0-9]*"), match he decimal point ("[.]"), then match exactly two digits ("[0-9]{2}"), and end the string ("$"). Put together, it looks like this:

^[0-9]*[.][0-9]{2}$

It does require that there are two digits after the decimal point, so three dollars cannot be entered as 3, but must be entered as 3.00 It can be made so that is acceptable.

Regex is extremely powerful and I don't think it's too difficult to learn. If you want to find or match on a pattern, there's nothing better. Google will give you lots of references. The only caveat is that there are minor variants depending on where you are using it, such as C#, Perl, Java, etc…These examples were tested in http://regexhero.net/tester which is C# compatible
 

Thread Starter

Dritech

Joined Sep 21, 2011
907
Thanks.

The problem is that it is not letting me compare the float data type.

This is how in am doing it:

The user is asked to enter a float (ex: 123.12)

I am reading the user input as follows: test = Convert.ToSingle(Console.ReadLine());
(where test is a variable of float data type)
 
Last edited:
Top