Skip to content

Instantly share code, notes, and snippets.

@cbmeeks
Created March 2, 2022 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbmeeks/133c294aeba0dbaeea85da7c926fce41 to your computer and use it in GitHub Desktop.
Save cbmeeks/133c294aeba0dbaeea85da7c926fce41 to your computer and use it in GitHub Desktop.
Valid Phone Number
package com.company;
public class Main {
public static void main(String[] args) {
String badNumber = "ABC123";
String goodNumber = "423-867-5309";
System.out.println("Testing Number " + badNumber);
System.out.println("\t" + isValidNumber(badNumber));
System.out.println("Testing Number " + goodNumber);
System.out.println("\t" + isValidNumber(goodNumber));
}
private static boolean isValidNumber(String number) {
String pattern = "(\\d-)?(\\d{3}-)?\\d{3}-\\d{4}";
if(number.matches(pattern))
return true;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment