Sorry the code is a bit messy; not sure why it isn't working

package com.codegym.task.task22.task2212;
/*
Phone number verification
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static boolean checkPhoneNumber(String phoneNumber) {
String[] digits = phoneNumber.split("");
int numbers = 0;
boolean startsPlus = false;
int dashes = 0;
int brackets = 0;
for (int i = 0; i < digits.length; i++){
if (i == 0)
if (digits[i].equals("+")){
startsPlus = true;
continue;
}
if (isNumeric(digits[i])) numbers++;
else if (digits[i].equals("-")){
if (i == digits.length-1 || digits[i+1].equals("-")) return false;
dashes++;
} else if (digits[i].equals("(")){
if (dashes>0 || i > digits.length-5 || !digits[i+3].equals(")")) return false;
brackets++;
} else if (digits[i].equals(")"))
if (dashes>0 && brackets!=1) return false;
else return false;
}
if (startsPlus)
if (numbers != 12) return false;
else
if (numbers != 10) return false;
return dashes <= 2 && brackets <= 1;
}
public static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch(NumberFormatException e){
return false;
}
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String s = bufferedReader.readLine();
boolean phoneNumber = checkPhoneNumber(s);
System.out.println(phoneNumber);
}
}

outsidethe loop and start the loop with i being at 1. There will only be one cycle where i equals 0, and considering it will be the least likely occurrence, checking for it every cycle is very inefficient. Also, if the string doesn't start with a plus sign, parentheses, or a number then you know the answer is already false and don't need the loop. #2 before the loop starts, check that the string ends with a number. You don't need to move any further if this is false as you would know the answer to the problem is false #3 Don't ever use the Exception system to solve a problem like that. It was not designed to do that and doing so is highly inefficient. The purpose is to ensure is to solve problems you don't know, as a way of tracing where the problems originate. In this case It would be like using a 5lb sledge hammer to hammer in regular 3" nails. Does it get the job done? Yes. Will you be able to do it all day? Not unless you are Hercules. In the case that you are using it for in this code using Character.isDigit() would be quite a bit more appropriate. This task is also made to be completed with regex statements. I highly recommend that you do a little research on them and apply them to this task. Here is a link to a place you can start: Regular Expressions