Validating Phone Numbers C# -


given list of phone numbers, let’s phone number 91 12 34 56, it’s not possible call right? because of emergency line.

i trying build program if user inputs phone number starts 911, output should "not consistent" otherwise "consistent".

here's code:

static void main(string[] args) {     var phonelist = new list<string>();     string input;     console.writeline("the phone number: ");      while ((input = console.readline()) != null)     {         phonelist.add(input);     }      (int = 0; < phonelist.count; i++)     {         (int j = 0; j < phonelist.count; j++)         {             if (phonelist[i].substring(0, 3).equals(phonelist[j].substring(0, 3)) && != j)             {                 console.writeline("not consistent");                 return;             }         }     }     console.writeline("consistent"); } 

my program jumps on if statement type 911. why that?

edit: phone number sequence of @ ten digits also!

you're kind of on right track. you've got couple of errors in code, both logical structurally.

first of all, while loop goes on forever because continues until input null... can't happen - @ best, input blank string "".

secondly, you're checking first 3 numbers entered of string (good) against first 3 numbers of every string in list (not good). instead, should checking "911".

static void main(string[] args) {     var phonelist = new list<string>();     string input;     console.writeline("the phone number: ");      while ((input = console.readline()) != "")     {         phonelist.add(input);     }          (int = 0; < phonelist.count; i++)     {         if (phonelist[i].substring(0, 3) == "911")         {             console.writeline("not consistent");             return;         }     }     console.writeline("consistent"); } 

it's important note, however, code not check special characters, whitespace, etc... assumes input 1234567890 instead of 12 34 56 78 90. if want rid of whitespace, make sure use string.replace(" ", "") on each of inputs before running them through loop.


Comments