Does anyone have an idea or why this exercise would be useful ?
I mean it's not like you have to churn out prep code and think about the logic of the program (unless you want to go the route of if statements like I started).
Is it worth trying to remember the regex combinations?
package com.codegym.task.task22.task2212;
/*
Phone number verification
*/
public class Solution {
public static boolean checkPhoneNumber(String phoneNumber) {
if (phoneNumber == null) return false;
/*
3) the number may contain 0-2 non-consecutive '-' characters
4) the number may contain 1 pair of matching parentheses, which must be to the left of the '-' characters
5) the parentheses must contain exactly 3 digits
7) the number must end with a digit
1) if the number starts with '+', then it must contain 12 digits
2) if the number starts with a digit or opening parenthesis, then it must contain 10 digits
*/
if (phoneNumber.length() < 10) return false;
if (phoneNumber.contains("+") && phoneNumber.indexOf("+") > 0) return false;
if (phoneNumber.contains("+") && phoneNumber.indexOf("+")==0 && phoneNumber.length() < 12) return false;
if (phoneNumber.contains("[a-z]")) return false;
if (phoneNumber.substring(phoneNumber.length()-1).equals("[a-z]")) return false;
return false;
}
public static void main(String[] args) {
}
}