So I want to make a social security class, and the input has to be in the following format
# # # - # # - # # # #
This is a string, and in my constructor i have the following code to check if its a proper method.
private static boolean checkSocialSecurityNumber(String s) {
int positionOfFirstDash = 3;
int positionOfSecondDash = 6;
if(s.length() != 11) {
return false;
}
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(i == positionOfFirstDash || i == positionOfSecondDash) {
if( c != ('-')){
return false;
}
}
else if(!Character.isDigit(c)) {
return false;
}
}
return true;
}
}
This method works, but its inside the classes constructor declaration.
and I would throw a system error if it returns false.
Is this the proper way to do it? it just looks wrong to have that in my constructor. My question is how I go about forcing the proper input, not sure how else to do it?