简体   繁体   English

使用 Java 验证电话号码

[英]Validating Phone number using Java

So I am creating a Java program that validates if a user has entered a valid phone number.所以我正在创建一个 Java 程序来验证用户是否输入了有效的电话号码。 I am using character array to store phone number.我正在使用character array来存储电话号码。 I believe that my Switch/Cases setup inside a for loop is causing a logical error and printing that the phone number is invalid although its valid.我相信我在 for 循环中的 Switch/Cases 设置会导致逻辑错误并打印出电话号码无效,尽管它是有效的。

As I am walking through the array I am searching for a character match;当我遍历数组时,我正在搜索字符匹配; if a match is not found then it is not a valid phone number .如果未找到匹配项,则它不是valid phone number

 for(int k =0; k < phoneNumArray.length; k++)
 {
 /* If 
/* Switch Case used to validae each array element*/
  switch(k)
  {
   case 0:
      if(phoneNumArray[0] != '(')
      {
       System.out.println("MISSING LEFT PARENTHESIS NOT A PHONE NUMBER");
       right_num = false;
      }break;
  case 1:
      if(phoneNumArray[1] == '0' || !(phoneNumArray[1]<= '1' || phoneNumArray[1]>= '9') || (phoneNumArray[1] >= 'A' || phoneNumArray[1] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
      }break;

   case 2:
      if(!(phoneNumArray[2] >= 0 || phoneNumArray[2] <= 9) || (phoneNumArray[2] >= 'A' || phoneNumArray[2] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
      }break;
    case 3:
      if(!(phoneNumArray[3] >= 0 || phoneNumArray[3] <= 9) || (phoneNumArray[3] >= 'A' || phoneNumArray[3] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
       break;
      };
   case 4:
      if(phoneNumArray[4] != ')')
   {
    System.out.println("MISSING RIGHT PARENTHESIS NOT A PHONE NUMBER");
    right_num = false;
    break;
   };
   case 5:
      if(!(phoneNumArray[5] >= 0 || phoneNumArray[5] <= 9) || (phoneNumArray[5] >= 'A' || phoneNumArray[5] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
      }break;
   case 6:
      if(!(phoneNumArray[6] >= 0 || phoneNumArray[6] <= 9) || (phoneNumArray[6] >= 'A' || phoneNumArray[6] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
      }break;
   case 7:
      if(!(phoneNumArray[7] >= 0 || phoneNumArray[7] <= 9) || (phoneNumArray[7] >= 'A' || phoneNumArray[7] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
      }break;
   case 8:
      if(phoneNumArray[8] != '-')
   {
    System.out.println("MISSING A DASH NOT A PHONE NUMBER");
    right_num = false;
   }break;
   case 9:
      if(!(phoneNumArray[9] >= 0 || phoneNumArray[9] <= 9) || (phoneNumArray[9] >= 'A' || phoneNumArray[9] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
      }break;
   case 10:
      if(!(phoneNumArray[10] >= 0 || phoneNumArray[10] <= 9) || (phoneNumArray[10] >= 'A' || phoneNumArray[10] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
      }break;
   case 11:
      if(!(phoneNumArray[11] >= 0 || phoneNumArray[11] <= 9) || (phoneNumArray[11] >= 'A' || phoneNumArray[11] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
      }break;
   case 12:
      if(!(phoneNumArray[12] >= 0 || phoneNumArray[12] <= 9) || (phoneNumArray[12] >= 'A' || phoneNumArray[12] <= 'z') )
      {
       System.out.println("MISSING VALID INTEGER");
       right_num = false;
      }break;

Note: expectation of 1 case is not clear in the question注意:问题中对1案例的期望不明确

  1. Unless performance is very important, it would be better to write regular expressions rather than trying to build a state machine for validations(whenever possible)除非性能非常重要,否则最好编写正则表达式而不是尝试构建 state 机器进行验证(尽可能)
  2. State machines will be difficult follow through and requires lot more code State 机器将难以跟进,需要更多代码
  3. Normal regular expressions without look head are easy to understand没有look head的普通正则表达式很容易理解
        String pattern = "^\\([1-9A-Za-z][0-9A-Za-z]{2}\\)[0-9A-Za-z]{3}-[0-9A-Za-z]{4}$";
        Pattern phoneNumber = Pattern.compile(pattern);
        System.out.println(phoneNumber.matcher("(000)000-0000").matches());
        System.out.println(phoneNumber.matcher("(100)000-0000").matches());

If the requirement was to use switch case, then如果要求是使用 switch case,那么

  1. Unify similar states into a single group将相似的状态统一到一个组中
  2. Write code for these unique groups为这些独特的组编写代码
        char[] phoneNumArray = "(100)000-0000".toCharArray();
        boolean right_num = true;
        for (int k = 0; k < phoneNumArray.length && right_num; k++) {
            final char current = Character.toUpperCase(phoneNumArray[k]);
            switch (k) {
                case 0:
                    if (current != '(') {
                        System.out.println("MISSING LEFT PARENTHESIS NOT A PHONE NUMBER");
                        right_num = false;
                    }
                    break;
                case 1:
                    if (current == '0' || !((current >= '1' && current <= '9') || (current >= 'A' && current <= 'Z'))) {
                        System.out.println("MISSING VALID INTEGER");
                        right_num = false;
                    }
                    break;

                case 2:
                case 3:
                case 5:
                case 6:
                case 7:
                case 9:
                case 10:
                case 11:
                case 12:
                    if (!((current >= '0' && current <= '9') || (current >= 'A' && current <= 'Z'))) {
                        System.out.println("MISSING VALID INTEGER");
                        right_num = false;
                    }
                    break;

                case 4:
                    if (current != ')') {
                        System.out.println("MISSING RIGHT PARENTHESIS NOT A PHONE NUMBER");
                        right_num = false;
                    }
                    break;
                case 8:
                    if (current != '-') {
                        System.out.println("MISSING A DASH NOT A PHONE NUMBER");
                        right_num = false;
                    }
                    break;
            }
        }
        System.out.println(right_num);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM