Hi, validator says that I do not check all verification criteria. Can you tell me what I'm missing? Meaby I don't understand the "\\d" regex
package com.codegym.task.task22.task2212;
/*
Phone number verification
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static boolean checkPhoneNumber(String phoneNumber) {
boolean isPhoneNumber = false;
char[] numberChars = phoneNumber.toCharArray();
int counter = 0;
if (containsLetters(phoneNumber)){
return false;
}
if (parenthes(phoneNumber)) {
return false;
}
for (char a : numberChars){
if (Character.toString(a).equals("-")){ counter++; }
}
if (counter > 2){
return false;
}else {
counter = 0;
}
if (!isLastDigit(numberChars)){
return false;
}
if (Character.toString(numberChars[0]).equals("+")){
Pattern pattern = Pattern.compile("\\d");
Matcher matcher = pattern.matcher(phoneNumber);
while (matcher.find()){
counter++;
}
if (counter == 12){
isPhoneNumber = true;
}
}else {
Pattern pattern = Pattern.compile("\\d");
Matcher matcher = pattern.matcher(phoneNumber);
while (matcher.find()){
counter++;
}
if (counter == 10){
isPhoneNumber = true;
}
}
return isPhoneNumber;
}
public static void main(String[] args) {
}
public static boolean containsLetters(String phoneNumber){
Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.find()){
return true;
}
return false;
}
public static boolean parenthes(String phoneNumber){
boolean openParenthes = phoneNumber.contains("(");
boolean closeParenthes = phoneNumber.contains(")");
if (!openParenthes ^ !closeParenthes){
return true;
}else if (!openParenthes && !closeParenthes){
return false;
}
int openingTimes = phoneNumber.length() - phoneNumber.replaceAll("\\(", "").length();
int closingTimes = phoneNumber.length() - phoneNumber.replaceAll("\\)", "").length();
if ((openingTimes + closingTimes) > 2){
return false;
}
int openIndeks = phoneNumber.indexOf("(");
int closeIndeks = phoneNumber.indexOf(")");
if ((closeIndeks - openIndeks) != 4){
return true;
}
int firstMinus = phoneNumber.indexOf("-");
if (firstMinus < openIndeks || firstMinus < closeIndeks){
return true;
}
return false;
}
public static boolean isLastDigit(char[] numberChars){
Pattern pattern = Pattern.compile("\\d");
Matcher matcher = pattern.matcher(Character.toString(numberChars[numberChars.length - 1]));
if (matcher.find()){
return true;
}
return false;
}
}