Solved

Java string capitalize testing

Posted on 2009-05-09
4
945 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 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