Question

(Obtaining Substrings) Write a program that prompts the user to enter full name contains the first...

(Obtaining Substrings) Write a program that prompts the user to enter full name contains the first name, middle name and last name separated by a space and extract the first name, middle name and last name from the input string.

Here is a sample run:

Enter the name: Dennis MacAlistair Ritchie

First Name: Dennis

Middle Name: MacAlistair

Last Name: Ritchie

JAVA

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ExtractName.java

/** java.util package is imported to use Scanner class to accept input from the user */
import java.util.Scanner;

/** Program to extract firstname, middlename and lastname from fullname */
public class ExtractName
{
public static void main(String[] args)
{

/** Create an object of Scanner class */
Scanner sc = new Scanner(System.in);

/** Accept full name from the user */
System.out.print("Enter FullName with FirstName, MiddleName, LastName separated by space :: ");
String name = sc.nextLine();


/** split method is used to divide a string into substrings based on any delimiter. Here the
* delimiter is space and hence it is used as name.split(" ").
* The method returns the substrings in a String array. The size of array is the number of
* substrings created by split method.
*/
String parts_of_name[] = name.split(" ");

/** Since three substrings are there in full name, they are available at locations 0,1,2*/
String firstname = parts_of_name[0];
String middlename = parts_of_name[1];
String lastname = parts_of_name[2];


/** Display the output */
System.out.println("\nFirst Name :: " + firstname);
System.out.println("Middle Name :: " + middlename);
System.out.println("Last Name :: " + lastname);
}
}

===================================================================================

Output:

Add a comment
Know the answer?
Add Answer to:
(Obtaining Substrings) Write a program that prompts the user to enter full name contains the first...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write Java code that prompts the user to enter his or her full name, then prints...

    Write Java code that prompts the user to enter his or her full name, then prints the name in reverse order (i.e., last name, first name). Read an entire line as a single string. Here is an example dialogue with the user: Please enter your full name: Sammy Jankis Your name in reverse order is Jankis, Sammy

  • Write a program that prompts the user to enter a list of names. Each person's name...

    Write a program that prompts the user to enter a list of names. Each person's name is separated from the next by a semi-colon and a space (: and the names are entered lastName, firstName (i.e. separated by ',). Your program should then print out the names, one per line, with the first names first followed by the last names. A sample run of your program should look like Please enter your list of names: Epstein, Susan; St. John, Katherine;...

  • JAVA Write a program that prompts the user to enter a year and the first three...

    JAVA Write a program that prompts the user to enter a year and the first three letters of a month name (with the first letter in uppercase) and displays the number of days in the month. If the input for month is incorrect, display a message as shown in the following sample run. SAMPLE RUN 1: Enter a year: 2001 Enter a month: Jan Jan 2001 has 31 days SAMPLE RUN 2: Enter a year: 2016 Enter a month: Feb...

  • Design a modular program using pseudo-code which prompts a user for their first name, followed by...

    Design a modular program using pseudo-code which prompts a user for their first name, followed by their last name; then displays a "Hello" salutation which concatenates their first name with their last name. Sample output (user input shown in red): Please enter your first name: John Please enter your last name: Smith Hello, John Smith! Your program must include a main module and one function; this function prompts the user for either their first name or last name, using a...

  • Write a program that separately prompts the user for a first name and last name and...

    Write a program that separately prompts the user for a first name and last name and outputs a string containing the following information, in order: a. First letter of the user's name. b. First five letters of the user's last name. c. A random two-digit integer You must construct the desired string ensuring all characters are lowercase; output the identification string accordingly. Assume the last name contains at least 5 characters. You must use the Random (java.util.Random) class to generate...

  • IN JAVA (Maximum consecutive increasingly ordered sub-string) Write a program that prompts the user to enter...

    IN JAVA (Maximum consecutive increasingly ordered sub-string) Write a program that prompts the user to enter a string and displays the maximum consecutive increasingly ordered sub-string. Analyze the time complexity of your program and explain. Here is a sample run: Enter a string: abcabcdgabxy output abcdg Time complexity is O( ). -n must be display between the parenthesis.

  • Using C Programming, Write a program that prompts for the user's first and last names. Declare a third array that will hold the user's last name and first name, separated by a comma and space....

    Using C Programming, Write a program that prompts for the user's first and last names. Declare a third array that will hold the user's last name and first name, separated by a comma and space. Use loops to inter through the names one character at a time, storing them into the full name array. Don't forget about the terminating character. Sample run: Enter your first name: john Enter your last name: matthews First name: John Last name: Matthews Full name:...

  • 1. Write a C++ program that will ask the user for their full name and calculate...

    1. Write a C++ program that will ask the user for their full name and calculate the user's weekly salary based on their hourly wage and the number of hours they worked, assuming no overtime at this point. Sample program run: Please enter first name: Elmer Please enter middle initial (Enter space if none): J Please enter last name: Fudd Enter hours worked: 37.5 Enter hourly rate: 10.35 Your name is: Elmer J. Fudd Your weekly salary is: $388.12 2....

  • problem2.cpp Write a program vhich asks the user to enter their full name (First Ni ddle...

    problem2.cpp Write a program vhich asks the user to enter their full name (First Ni ddle Last, using getline()) and then ei ther either prints out their middle name, or prints "Error: no midd le name" if they entered a name vithout a middle name (1ess than tvo spaces). 6 8#include <iostream> 9 #include <string 18 using namespace std; 11

  • Write a program that prompts the user to enter a file name and displays the occurrences...

    Write a program that prompts the user to enter a file name and displays the occurrences of each letter in the file. Letters are case-insensitive. Here is a sample run: Enter a filename: Lincoln.txt Number of A’s: 23 Number of B’s: 0 Number of C’s: 12 …… Number of Y’s: 5 Number of Z’s: 7

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT