I've tried testing my code with the examples provided in the conditions and it doesn't work.
I'm not sure that I've used the escape characters properly.
Also, line 16
if (temp.length() != 10) return false;
has a warning "Condition is always true", and I'm not sure whypackage com.codegym.task.task22.task2212;
/*
Phone number verification
*/
public class Solution {
public static boolean checkPhoneNumber(String phoneNumber) {
//check amount of digits
String temp = phoneNumber.replaceAll("[^0-9]", "");
if (phoneNumber.startsWith("+"))
if (temp.length() != 12) return false;
else
if (temp.length() != 10) return false;
//check amount of -
temp = phoneNumber.replaceAll("-", "");
if (temp.length() > 2) return false;
//check placement of -
if (phoneNumber.matches("\\.*--\\.*")) return false;
//check amount of ()
temp = phoneNumber.replaceAll("\\(", "");
if (temp.length() > 1) return false;
//check placement of ()
if (phoneNumber.contains("(") && !phoneNumber.matches("\\.*\\(\\d{3}\\)\\.*")) return false;
if (phoneNumber.matches("\\.*-\\.*\\(\\.*")) return false;
//check if contains letters and ends with digit
if (phoneNumber.contains("[a-zA-Z") || !phoneNumber.endsWith("\\d")) return false;
return true;
}
public static void main(String[] args) {
boolean test = checkPhoneNumber("+38(050)1234567");
System.out.println(test);
}
}