Created
March 2, 2022 16:19
-
-
Save cbmeeks/133c294aeba0dbaeea85da7c926fce41 to your computer and use it in GitHub Desktop.
Valid Phone Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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