I keep getting StringIndexOutOfBoundsException for this part of the code. What could be the reason? contact is declared as String
do{
System.out.print("Contact Number (01X-XXXXXXX) :");
contact = scan.next();
if(!phNumValidation(contact)){
System.out.println("Invalid Phone Number. Please try again.");
System.out.println("");
}
}while(!phNumValidation(contact) || contact.length() < 11);
this is the method for phone number validation
public static boolean phNumValidation(String contact){
boolean valid = true;
String dash = contact.substring(3, 4);
if(contact.length() == 11){
valid = true;
valid = contact.startsWith("01");
valid = dash.matches("-");
}
else{
valid = false;
}
return valid;
}