c# filtering user input

Discussion in 'Programmer's Corner' started by Dritech, Dec 30, 2014.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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.
     
  2. vpoko

    Member

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

    For example:
    Code (Text):
    1.  
    2. do
    3. {
    4. Console.Write("Enter Name: ");
    5. name = Console.ReadLine();
    6. }
    7. while (!isValid(name));
    8.  
    isValid() is a function you'd define to validate the name.
     
  3. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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?
     
  4. sirch2

    Well-Known Member

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

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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?
     
  6. vpoko

    Member

    Jan 5, 2012
    258
    47
    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.
     
    Dritech likes this.
  7. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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
     
    Dritech likes this.
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    By that time, you might as well use a dictionary...

    Are you using the Add method to add to the list?
     
  9. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks for the replies. I will try these methods.
     
  10. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi, yes I am using the .add method to add to the list.
     
  11. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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.
     
  12. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Look as the insert method if you really want to do it that way.
     
  13. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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.
     
  14. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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?
     
  15. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,802
    832
    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.
     
  16. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,802
    832
    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 ("$")
     
    Dritech and tshuck like this.
  17. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Thanks a lot for the reply :)
     
  18. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Can I use Regex.IsMatch to only accept float data type? (example for prices)
     
  19. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,802
    832
    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
     
  20. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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: Jan 3, 2015
Loading...