Solved

Java string capitalize testing

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 86
Core java. Create the class with method that do logic for all elements of collection. 1 69
java 8 lambda expresssions exception handling 3 75
Java Loop 6 49
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

920 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