Uppercase
Hi,
Please can you tell me if there is a method to that checks forfirst letter of the word to uppercase.
Example:
If user enters his name: john smith
Using stringtokenizer and uppercase or lowercase I'm checking complete word
if its in upper or lower case.
I want user to start his name asJohn Smith.
Tks
RJ
so what happens if the user's name isn't supposed to be capitalized? Not all cultures capitalize every word in their name.
A very common thing is to have a linking word before the last name. Often, this word will not be capitalized.
If you still think you should do this, though, no, I don't think there is any method in the libraries that do this for you, you will have to do what you described:
tokenize the string (look into StringTokenizer or String.split)
get the first character of the string (String.charAt(0))
check if it is a capital
- Adam
i love String
package cruft;
import java.util.Scanner;
public class CapitalString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
String name;
String space = " ";
s.useDelimiter(System.getProperty("line.separator"));
System.out.println("Enter your name please");
name = s.next();
String firstName = name.substring(0,name.indexOf(space));
String capitalFirst = firstName;
capitalFirst = firstName.substring(0,1);
firstName = name.substring(1,name.indexOf(space));
String lastName = name.substring(name.indexOf(space)+1, name.length());
String capitalLast = lastName;
capitalLast = lastName.substring(0,1);
name = name.substring(name.indexOf(space)+1, name.length());
lastName = name.substring(1,name.length());
System.out.println(capitalFirst.toUpperCase()+""+firstName+" "+capitalLast.toUpperCase()+""+lastName);
}
}
Not recommended but was too bored so thought to use thw power of substring and indexof :)
> firstName = name.substring(1,name.indexOf(space));
>String lastName = name.substring(name.indexOf(space)+1, name.length());
hm...
So what if somebody enters "George Walker Bush"? Then, apparently, the President's last name is "Walker Bush". How strange that his wife doesn't go by "Walker Bush"!
- Adam
If what you really want is a first name and a last name, then you should ask for each separately. And the onus should be on the client to capitalize it correctly, because last names that are not English often do not capitalize the first letter (sometimes it is a different letter, or sometimes, none at all).
- Adam