Java programming Exercises: Validate a given phone number
Java Regular Expression: Exercise-22 with Solution
Write a Java program to validate a given phone number.
Following are valid phone number examples:
“(123)4567890", "1234567890", "123-456-7890", "(123)456-7890",
Following are invalid phone numbers:
"(1234567890)","123)4567890", "12345678901", "(1)234567890", "(123)-4567890", "1", "12-3456-7890", "123-4567", "Hello world"};
- Regex explanation:
- ^\\(? - May start with an option "("
- (\\d{3}) - Followed by 3 digits
- \\)? - May have an optional ")"
- [- ]? - May have an optional "-" after the first 3 digits or after optional ) character
- (\\d{3}) - Followed by 3 digits.
- [- ]? - May have another optional "-" after numeric digits
- (\\d{4})$ - ends with four digits
Sample Solution:
Java Code:
//Ref.https://bit.ly/33gB1TY
public class test {
public static void main(String[] args) {
String text ="(123)4567890";
System.out.println("Original Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="(123)4567890";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="1234567890";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="123-456-7890";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="(1234567890)";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="123)4567890";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="12345678901";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="(1)234567890";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="(123)-4567890";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="1";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="12-3456-7890";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
text ="123-4567";
System.out.println("\nOriginal Phone number: "+text);
System.out.println("Check the said Phone number is true or not! "+validate(text));
}
public static Boolean validate(String text) {
return text.matches("\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}");
}
}
Sample Output:
Original Phone number: (123)4567890 Check the said Phone number is true or not! true Original Phone number: (123)4567890 Check the said Phone number is true or not! true Original Phone number: 1234567890 Check the said Phone number is true or not! true Original Phone number: 123-456-7890 Check the said Phone number is true or not! true Original Phone number: (1234567890) Check the said Phone number is true or not! false Original Phone number: 123)4567890 Check the said Phone number is true or not! false Original Phone number: 12345678901 Check the said Phone number is true or not! false Original Phone number: (1)234567890 Check the said Phone number is true or not! false Original Phone number: (123)-4567890 Check the said Phone number is true or not! false Original Phone number: 1 Check the said Phone number is true or not! false Original Phone number: 12-3456-7890 Check the said Phone number is true or not! false Original Phone number: 123-4567 Check the said Phone number is true or not! false
Flowchart :
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Remove all non-alphanumeric characters from a given string.
Next: Move all lower case letters to the front, keeping the order of all the letters of a given word.What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
What is the best way to filter a Java Collection?
Java 8 (2014) solves this problem using streams and lambdas in one line of code:
List<Person> beerDrinkers = persons.stream() .filter(p -> p.getAge() > 16).collect(Collectors.toList());
Use Collection#removeIf to modify the collection in place. (Notice: In this case, the predicate will remove objects who satisfy the predicate):
persons.removeIf(p -> p.getAge() <= 16);
lambdaj allows filtering collections without writing loops or inner classes:
ListbeerDrinkers = select(persons, having(on(Person.class).getAge(), greaterThan(16)));
Ref: https://bit.ly/3uwYid6
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework