90.7k views
3 votes
Write a program in which will use some of the built-in METHODS as it relates to the use of Strings. 1) The program will ask the user the following prompts: Please enter your first name and their last name, separated by a space. This program will then read-in the user's response using Scanner. You must separate the string input entered by the user into two strings via substrings. Substring one will store the first name and Substring two will store the last name respectively. ***hint this can be accomplish using the indexOf() to find the position of the space. Your program must display/output the number of characters in each name, as well as the user's initials that was entered. You must check and trim all extra white spaces before and after the string entered by the user. Incorporate and loop to have this program ask this user if they would like to execute the program again by prompting and requesting a Y for Yes and a N for No. This prompt should ignore upper or lower case entries. The initials are the first letter of the first name together with the first letter of the last name. Hello their Louis I have your first name as Louis, which has 5 characters Hello again Louis I have your last name as Henry, which has 5 characters Did you know that your initials are LH Do you wish to continue (Y)Yes or (N)No

1 Answer

2 votes

Answer:

The program in Java is as follows:

import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

char cont = 'Y';

while(cont == 'Y'){

System.out.print("Name: ");

String name =input.nextLine();

String names [] = name.split(" ");

names[0] = names[0].replaceAll("\\s", "");

names[1] = names[1].replaceAll("\\s", "");

System.out.println("Hello their "+names[0]+", I have your first name as "+names[0]+", which has "+names[0].length()+" characters ");

System.out.println("Hello again "+names[0]+", I have your last name as "+names[1]+", which has "+names[1].length()+" characters ");

System.out.println("Did you know that your initials are "+names[0].charAt(0)+names[1].charAt(0));

System.out.print("Continue (Y)Yes or (N)No: ");

cont = Character.toUpperCase(input.next().charAt(0));

input.nextLine();} }}

Step-by-step explanation:

This initializes a character variable cont to Y

char cont = 'Y';

This is repeated as long as cont is Y

while(cont == 'Y'){

This prompts the user for name

System.out.print("Name: ");

This gets the name from the user

String name =input.nextLine();

This splits the name into the array of two elements (first name and last name)

String names [] = name.split(" ");

This remove all whitespace from the first name

names[0] = names[0].replaceAll("\\s", "");

This remove all whitespace from the last name

names[1] = names[1].replaceAll("\\s", "");

This prints the details of the first name

System.out.println("Hello their "+names[0]+", I have your first name as "+names[0]+", which has "+names[0].length()+" characters ");

This prints the details of the last name

System.out.println("Hello again "+names[0]+", I have your last name as "+names[1]+", which has "+names[1].length()+" characters ");

This prints the initials

System.out.println("Did you know that your initials are "+names[0].charAt(0)+names[1].charAt(0));

This prompts the user to continue or not

System.out.print("Continue (Y)Yes or (N)No: ");

This gets the user response

cont = Character.toUpperCase(input.next().charAt(0));

input.nextLine();} }

User Kazade
by
3.2k points