Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java验证电话号码_Java_Arrays_Switch Statement - Fatal编程技术网

使用Java验证电话号码

使用Java验证电话号码,java,arrays,switch-statement,Java,Arrays,Switch Statement,因此,我正在创建一个Java程序,用于验证用户是否输入了有效的电话号码。 我正在使用字符数组存储电话号码。 我相信我在for循环中的开关/机箱设置导致了逻辑错误,并打印出电话号码无效,尽管它是有效的 当我在数组中行走时,我正在搜索字符匹配;如果未找到匹配项,则它不是有效的电话号码 for(int k =0; k < phoneNumArray.length; k++) { /* If /* Switch Case used to validae each array element

因此,我正在创建一个Java程序,用于验证用户是否输入了有效的电话号码。 我正在使用
字符数组
存储电话号码。 我相信我在for循环中的开关/机箱设置导致了逻辑错误,并打印出电话号码无效,尽管它是有效的

当我在数组中行走时,我正在搜索字符匹配;如果未找到匹配项,则它不是有效的电话号码

 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;
for(int k=0;k(phoneNumArray[1];(PhoneNuNuMarray[1];;;(PhoneNuNuMarray[1];(PhoneNuNuMarray[1]>='A’|;;;phoneNumArray[1]=若若若(1)若若若若若若若(1)若若若(PhoneNuNuMarraray[1)1]==='0'0'0'0'0''0'0'0’\[1[1)若若若(1)若(1)若)若若若若有有上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述上述[1[1];;若若[1]1];;若若[1]1]===========0 取取取取取取取取取取取取取取取取取取取取取取取取phoneNumArray[6]=0 | | phoneNumArray[7]=A | | phoneNumArray[7] =0 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |

  • 除非性能非常重要,否则最好编写正则表达式,而不是尝试构建用于验证的状态机(只要可能)
  • 状态机很难实现,需要更多的代码
  • 没有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();
    
    如果要求使用开关箱,则

  • 将相似的状态统一为一个组
  • 为这些独特的组编写代码
  • char[]phoneNumArray=“(100)000-0000”.toCharArray();
    布尔右_num=真;
    对于(int k=0;k如果(current='0'| |!((current>='1'&¤t='A'&¤t='0'&¤t='A'&¤t),请提供您的示例输入。理解。感谢您的帮助!!