humberss
asked on
Java string capitalize testing
Im inputting a users name as 2 strings first name last name and I want to test if the first letter of their first name and last name is capitalize and only the first letter if not it needs to print an error message for the user to correct the problem.
It sounds to me like you are storing their first and last name seperately, so it would be more like this:
if( Character.isLowerCase(firs tName.char At(0)) )
{
//Print error
}
else
{
if( Character.isLowerCase( lastName.charAt(0) )
{
//print error
}
}
if( Character.isLowerCase(firs
{
//Print error
}
else
{
if( Character.isLowerCase( lastName.charAt(0) )
{
//print error
}
}
Some names have caps that aren't the first letter, but if you want to force it you could do the first bit of code.
If you just want to check use a Pattern and Match
If you just want to check use a Pattern and Match
firstName = firstName.substring(0,1).toUpperCase() + firstName.substring(1).toLowerCase();
Matcher m = Pattern.compile("^[A-Z]+[a-z]*$").matcher(firstName);
if(m.matches())
{
System.out.println("valid");
}
else
{
System.out.println("invalid");
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
String[] names = input.split(" ");
you can use the Character class and the String charAt() method to check
char c first = names[0].charAt(0);
if (Character.isLowerCase(fir
{
// first character is lower case
do the same for the 2nd name