Please help me find the wrong reason.Thanks a lot.
package com.codegym.task.task22.task2212;
/*
Phone number verification
*/
public class Solution {
public static boolean checkPhoneNumber(String phoneNumber)
{
char[] chars = phoneNumber.toCharArray();
int countNum = 0;
int countChar = 0;
int countLeft = 0;
int countRight = 0;
for(char ch:chars)
{
if(Character.isDigit(ch))
{
countNum++;
}
if(ch == '-')
{
countChar++;
}
if(ch== '(')
{
countLeft++;
}
if(ch == ')')
{
countRight++;
}
if(Character.isAlphabetic(ch))
{
return false;
}
}
if(chars[0] =='+')
{
if(countNum!=12)
{
return false;
}
}
else
{
if(countNum!=10)
{
return false;
}
}
if(countChar>2)
{
return false;
}
if(countLeft>1)
{
return false;
}
if(countRight>1)
{
return false;
}
if(countLeft!=countRight)
{
return false;
}
if(!Character.isDigit(chars[chars.length-1]))
{
return false;
}
if(countLeft!=0)
{
int leftParent = 0;
int rightParent = 0;
int first_ = 0;
for(int i = 0;i<chars.length;i++)
{
if(chars[i]=='(')
{
leftParent = i;
}
if(chars[i] == ')')
{
rightParent = i;
}
if(chars[i] == '-' && first_==0)
{
first_ = i;
}
}
if(rightParent-leftParent!=4 || rightParent > first_)
{
return false;
}
for(int i = leftParent+1;i<rightParent;i++)
{
if(!Character.isDigit(chars[i]))
{
return false;
}
}
}
return true;
}
public static void main(String[] args) {
System.out.println(checkPhoneNumber("(5_0)85-23-4567"));
}
}