Java: Programming: Initials 1
Name ________________________________________
Write a program that asks for names and displays the initials.
- Ask the user for their first name. Save it in a string variable.
- Ask the user for their last name. Save it in a string variable.
- Use the substring() method to get the first characters from both the first and last names. Use the toUpperCase() method to convert them to upper case.
- Display the initials to the user.
Discussion
What you need is the first letter of each name. There are two ways to get it.
-
substring(...). You can use thesubstring(...)method to get a one-character substring starting at the beginning of the name. You can then concatenate this to the first letter from the other name. charAt(...)Strings are made up of characters (primitive type char). You can extract the first character withcharAt(...)and proceed pretty much as above.
Which to use? They are both reasonable solutions.
If you were doing a huge amount of computation -- processing hundreds of thousands of names,
it would be more efficient to use the charAt(...) solution because it's more
efficient to use a primitive type than an object type.
On the other hand, the substring(...) solution has the simplicity
of working with only one type (String).
Use either one.















