I have written some code in Java that checks if the strings Firstname
and Lastname
are valid names. These are inputted by the user. My code works as expected, but I would like to optimize and shorten the code such that it is readable and understandable.
Both these strings, Firstname
and Lastname
, will not be null
or empty and might contain more than one word.
Firstname
and Lastname
are valid if
- The length of both
Firstname
andLastname
are more than two. - There isn't any character which isn't an alphabet or space in them.
- There isn't more than three vowels in a word in
Firstname
orLastname
, and there isn't more than two consecutive vowels in a word. - There isn't more than four consecutive consonants in a word in
Firstname
orLastname
, and there isn't more than two consecutive same consonant in a word.
The below code works as expected:
/*Returns false if both names are valid and true if they aren't*/
public static boolean errcheck(String Firstname,String Lastname,int security)
{
while(true)
{
if(security>=2 && !checklen(Firstname,Lastname))break;
if(security>=3 && !checknum(Firstname,Lastname))break;
if(security>=4 && !checkvowel(Firstname,Lastname))break;
if(security>=5 && !checkcon(Firstname,Lastname))break;
return true;
}
return false;
}
/*Checks if length of both strings are less than three*/
public static boolean checklen(String Firstname,String Lastname)
{
if(Firstname.length()<3 || Lastname.length()<3)
return false;
return true;
}
/*Checks if both strings contain non-alphabetic characters excluding a space character*/
public static boolean checknum(String Firstname,String Lastname)
{
int length=0,length2=0,i=0;
char c;
String Fullname;
length=Firstname.length();
length2=Lastname.length();
for(i=0;i<length;i++) //Check for `Firstname`
{
c = Firstname.charAt(i);
if(!((c>='a' && c<='z')||(c>='A' && c<='Z')) && c!=' ')
{
return false;
}
}
for(i=0;i<length2;i++) //Check for `Lastname`
{
c = Lastname.charAt(i);
if(!((c>='a' && c<='z')||(c>='A' && c<='Z')) && c!=' ')
{
return false;
}
}
return true;
}
/*Checks if both strings contain more than 2 consecutive vowels. Also checks if they contain more than 3 vowels. Both are done in each word*/
public static boolean checkvowel(String Firstname,String Lastname)
{
int numv=0,conv=0,length=Firstname.length(),length2=Lastname.length();
char c;
int i;
char[] vowel={'a','e','i','o','u','A','E','I','O','U','\0'};
for(i=0;i<length;i++){
c=Firstname.charAt(i);
for(char test:vowel)
{
if(c!=' ')
{
if(test==c) //If current char is a vowel
{
numv++;
conv++; //Increase counters
if(conv>2 || numv>3) //Invalid name detected
return false;
break;
}
if(test=='\0')conv=0;
}else{numv=conv=0;break;} //New word. So reset counters
}
}
numv=conv=0;
for(i=0;i<length2;i++){ //Do the same for `Lastname`
c=Lastname.charAt(i);
for(char test:vowel)
{
if(c!=' ')
{
if(test==c)
{
numv++;
conv++;
if(conv>2 || numv>3)
return false;
break;
}
if(test!='\0')conv=0;
}else{numv=conv=0;break;}
}
}
return true;
}
/*Checks if both strings contain more than 2 consecutive same consonants. Also checks if they contain more than 4 consonants. Both are done in each word*/
public static boolean checkcon(String Firstname,String Lastname)
{
int num=0,length=Firstname.length(),length2=Lastname.length(),con=0;
char c,tmp='a';
int i;
for(i=0;i<length;i++)
{
if((c=Firstname.charAt(i))!='a' && c!='e' && c !='i' && c!='o' && c!='u' && c!='A' && c!='E' && c !='I' && c!='O' && c!='U' && c!=' ') //If current character is not a vowel or a space
{
num++;
if(tmp!='a' && c==tmp)
con++;
if(num>3 || con>1) //Invalid name
return false;
tmp=c;
continue;
}
num=0;
con=0;
tmp='a';
}
num=0;
con=0;
tmp='a'; //Reset everything
for(i=0;i<length2;i++) //Do the same check for `Lastname`
{
if((c=Lastname.charAt(i))!='a' && c!='e' && c !='i' && c!='o' && c!='u' && c!='A' && c!='E' && c !='I' && c!='O' && c!='U' && c!=' ')
{
num++;
if(tmp!='a' && c==tmp)
con++;
if(num>3 || con>1)
return false;
tmp=c;
continue;
}
num=0;
con=0;
tmp='a';
}
return true;
}
I would like to shorten and optimize the above code.