Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please use JAVA Write a public static method named nameToInitials that will take a single argument of type String and will return a String. When
Please use JAVA
Write a public static method named nameToInitials that will take a single argument of type String and will return a String. When called, and passed a name, this method must compute and return a String value with the initials for the given name.
Here are some examples showing what the value returned by your nameToInitials method should be for different argument values:
nameToInitialsBob Allen Smith" should return the String value B A S
nameToInitialsBob Smith" should return the String value B S
nameToInitialsBob should return the String value B
Note that the argument to this method may be a String containing just a first name ie "Bob" just first name and last name ie "Bob Smith" or the first name and middle name and last name ie "Bob Allen Smith"
You must also write a small program in the main method that will:
prompt the user to enter a name
collect and store the user's input in a String variable
call the nameToInitials method, passing the user's input as an argument
store the value returned by the nameToInitials method in another String variable
Display the name and initials as shown in the examples below
Example
Enter name : Bob Allen Smith
Initials for Bob Allen Smith : B A S
Example
Enter name : Bob Smith
Initials for Bob Smith : B S
Example
Enter name : Bob
Initials for Bob : B
For the given inputs, make sure that your program output looks exactly like the example above including spelling, capitalization, punctuation, spaces, and decimal points
Hints and Helpful Info
The indexOf method can be very helpful for finding things like space characters in a String
Java has an overloaded indexOf method with takes an extra int argument indicating where to start a search in a String
Java also has an lastIndexOf method, which searches a String starting at the end.
The substring method can be very helpful for extracting one or more characters from a String
You may need to wrtie some to code to make decisions based on whether the input has one just first two first and last or three first middle, and last names.
The number of spaces in the input can help you determine how many names are in the input.
It can be helpful to know and understand the difference between the Scanner methods next and nextLine
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started