Solved

Java string capitalize testing

Posted on 2009-05-09
4
953 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

691 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