Solved

Java string capitalize testing

Posted on 2009-05-09
4
938 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:humberss
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24343119
You can use the split() method to break up first and last name

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(first))
{
   // first character is lower case


do the same for the 2nd name

0
 

Expert Comment

by:Weetbixiron
ID: 24343231
It sounds to me like you are storing their first and last name seperately, so it would be more like this:

if( Character.isLowerCase(firstName.charAt(0)) )
{
       //Print error
}
else
{
      if( Character.isLowerCase( lastName.charAt(0) )
     {
             //print error
     }
}
0
 
LVL 3

Expert Comment

by:hazgoduk
ID: 24343298
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
        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");

        }

Open in new window

0
 
LVL 1

Accepted Solution

by:
mohgupta earned 500 total points
ID: 24382718
you can also try this
String firstname ="Abc";

String lastname = "Xyz";

char ch= firstname.charAt(0);

if(! Character.isUpperCase(ch)){

System.out.println("Please enter first letter of firstname in uppercase");

}

ch=lastname.charAt(0);

if(! Character.isUpperCase(ch)){

System.out.println("Please enter first letter of lastname in uppercase");

}

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now