Write a static method called "initials" that takes as a parameter a String containing a persons name. For example, the following call: initials("Marie Elain Johnson") should return "MEJ". Initials are formed by combining the capitalized first letters of each name component. Components in each name will be separated by spaces, tabs, or the hyphen character. There might be extra spaces at the beginning or end of the phrase. You may assume that the name String will have at least one name component. You must extract the first letter of each name component and capitalize if needed and use that to build the name’s initials. In the event of a hyphenated name, like Wilson-Smith, you must first replace the hyphen with a space character You may use the following code to test your program: Expected output is listed to the right of the method calls. import java.util.*; public class Initials { public static void main(String[] args) { System.out.println(initials("John Allen Jay")); // "JAJ" System.out.println(initials(" Lisa Wilson")); // "LW" System.out.println(initials(" Quinton ")); // "Q" System.out.println(initials("lisa ann marie Kiley-Marsh")); // "LAMKM" } // *** Your method code goes here *** } // End of Initials class
Write a static method called "initials" that takes as a parameter a String containing a persons name. For example, the following call: initials("Marie Elain Johnson") should return "MEJ".
Initials are formed by combining the capitalized first letters of each name component. Components in each name will be separated by spaces, tabs, or the hyphen character. There might be extra spaces at the beginning or end of the phrase. You may assume that the name String will have at least one name component.
You must extract the first letter of each name component and capitalize if needed and use that to build the name’s initials. In the event of a hyphenated name, like Wilson-Smith, you must first replace the hyphen with a space character
You may use the following code to test your program:
Expected output is listed to the right of the method calls.
import java.util.*;
public class Initials {
public static void main(String[] args) {
System.out.println(initials("John Allen Jay")); // "JAJ"
System.out.println(initials(" Lisa Wilson")); // "LW"
System.out.println(initials(" Quinton ")); // "Q"
System.out.println(initials("lisa ann marie Kiley-Marsh")); // "LAMKM"
}
// *** Your method code goes here ***
} // End of Initials class
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images