Answered step by step
Verified Expert Solution
Link Copied!

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:
nameToInitials("Bob Allen Smith") should return the String value "B. A. S."
nameToInitials("Bob Smith") should return the String value "B. S."
nameToInitials("Bob") should return the String value "B."
Note that the argument to this method may be a String containing just a first name (i.e. "Bob"), just first name and last name (i.e. "Bob Smith"), or the first name and middle name and last name (i.e. "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 1
Enter name : Bob Allen Smith
Initials for Bob Allen Smith : B. A. S.
Example 2
Enter name : Bob Smith
Initials for Bob Smith : B. S.
Example 3
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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions

Question

=+1.5. 1 The Cantor set C can be defined as the closure of A3(1).

Answered: 1 week ago

Question

3. Identify cultural universals in nonverbal communication.

Answered: 1 week ago