Programmer_Novice
Programmer_Novice

Reputation: 13

Validating Phone number using Java

So I am creating a Java program that validates if a user has entered a valid phone number. I am using character array to store phone number. 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.

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.

 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;

Upvotes: 1

Views: 160

Answers (1)

Thiyanesh
Thiyanesh

Reputation: 2360

Note: expectation of 1 case is not clear in the question

  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)
  2. State machines will be difficult follow through and requires lot more code
  3. Normal regular expressions without look head are easy to understand
        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

  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);

Upvotes: 1

Related Questions