Wednesday, February 21, 2024
HomeSoftware EngineeringMethods to Validate Cellphone Numbers in Java

Methods to Validate Cellphone Numbers in Java


The problem

Write a perform that accepts a string, and returns true whether it is within the type of a cellphone quantity.
Assume that any integer from 0-9 in any of the spots will produce a legitimate cellphone quantity.

Solely fear in regards to the following format:
(123) 456-7890 (don’t neglect the house after the shut parentheses)

Examples:

"(123) 456-7890"  => true
"(1111)555 2345"  => false
"(098) 123 4567"  => false

The answer in Java code

Choice 1:

public class Resolution {
  public static boolean validPhoneNumber(String phoneNumber) {
    return phoneNumber.matches("(d{3}) d{3}-d{4}");
  }
}

Choice 2:

public class Resolution {
  public static boolean validPhoneNumber(String phoneNumber) {
      char[] cStr = phoneNumber.toCharArray();
      if(cStr[0] != '(') return false;
      if(cStr[4] != ')') return false;
      if(cStr[5] != ' ') return false;
      if(cStr[9] != '-') return false;
            
      for(int i = 1; i < cStr.size; i++){
         if(i != 4 && i != 5 && i != 9){
            if(!Character.isDigit(cStr[i])) return false;
         }
      }
      return true;
  }
}

Choice 3:

public class Resolution {
  public static boolean validPhoneNumber(String phoneNumber) {
    return phoneNumber.equalsIgnoreCase("(123) 456-7890");
  }
}

Check instances to validate our answer

import org.junit.Check;
import static org.junit.Assert.assertEquals;

public class PhoneExampleTests {
  @Check
  public void checks() {
    assertEquals(true, Resolution.validPhoneNumber("(123) 456-7890"));
    assertEquals(false, Resolution.validPhoneNumber("(1111)555 2345"));
    assertEquals(false, Resolution.validPhoneNumber("(098) 123 4567"));
  }
}

Extra check instances

import org.junit.Check;
import static org.junit.Assert.assertEquals;

public class PhoneTests {
  @Check
  public void basicTests() {
    String msg = "Comply with the formatting directions rigorously";
    assertEquals(msg, true, Resolution.validPhoneNumber("(123) 456-7890"));
    assertEquals(msg, false, Resolution.validPhoneNumber("(1111)555 2345"));
    assertEquals(msg, false, Resolution.validPhoneNumber("(098) 123 4567"));
    assertEquals(msg, false, Resolution.validPhoneNumber("(123)456-7890"));
  }
  
  @Check
  public void formCharTests() {
    String msg = "Take note of the formatting of the string and surrounding characters";
    assertEquals(msg, false, Resolution.validPhoneNumber("abc(123)456-7890"));
    assertEquals(msg, false, Resolution.validPhoneNumber("(123)456-7890abc"));
    assertEquals(msg, false, Resolution.validPhoneNumber("abc(123)456-7890abc"));
  }
  
  @Check
  public void charTests() {
    String msg = "Watch out with characters surrounding the cellphone quantity";
    assertEquals(msg, false, Resolution.validPhoneNumber("abc(123) 456-7890"));
    assertEquals(msg, false, Resolution.validPhoneNumber("(123) 456-7890abc"));
    assertEquals(msg, false, Resolution.validPhoneNumber("abc(123) 456-7890abc"));
  }
  
  @Check
  public void badCharTests() {
    String msg = "Watch out with non-digit characters inside cellphone quantity";
    assertEquals(msg, false, Resolution.validPhoneNumber("(123) 456-78f0"));
    assertEquals(msg, false, Resolution.validPhoneNumber("(123) 4e6-7890"));
    assertEquals(msg, false, Resolution.validPhoneNumber("(*23) 456-7890"));
  }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments