Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program call FancyMyName which asks you to write a program that tests an

ID: 3717777 • Letter: W

Question

Write a program call FancyMyName which asks you to write a program that tests and using thedifferent methods for working with Strings.

This program will ask the user to enter their first name and their last name, separated by a space.

Read the user's response using Scanner. Seperate the input string up into two strings, one containing the first name and one containing the last name.You can accomplish this by using the indexOf() hint*** find the position of the space, and then using substring() to extract each of the two names. Also output the number of characters in each name, and output the user's initials. The initials are the first letter of the first name together with the first letter of the last name.

A sample run of the program should look something like this:

Please enter your first name and last name, separated by a space?

Explanation / Answer

import java.util.Scanner;

public class FancyMyName {

public static void main(String args[]) {

//Declare the variables

String name, firstName, lastName, initials;

int numCharactersFirst, numCharactersLast;

//Initialize Scanner to read input string from user

Scanner sc = new Scanner(System.in);

boolean flag = false;

//Prompt user to input Name

System.out.println("Enter your name: ");

name = sc.nextLine();

//Perform the FancyName

int index = name.indexOf(" ");

if(index == -1)

{

System.out.println("Please Input your complete name in the format (Firstname Lastname)");

name = sc.nextLine();

flag = true;

}

else{

firstName = name.substring(0, index);

lastName = name.substring(index+1);

numCharactersFirst = firstName.length();

numCharactersLast = lastName.length();

initials = firstName.substring(0, 1) + lastName.substring(0, 1);

//Display the results

System.out.println("First Name: "+firstName+" Last Name: "+lastName);

System.out.println("Initials: "+initials);

System.out.println("Number of Characters in First Name: "+numCharactersFirst);

System.out.println("Number of Characters in Last Name: "+numCharactersLast);

}

if(flag==true)

{

index = name.indexOf(" ");

firstName = name.substring(0, index);

lastName = name.substring(index+1);

numCharactersFirst = firstName.length();

numCharactersLast = lastName.length();

initials = firstName.substring(0, 1) + lastName.substring(0, 1);

//Display the results

System.out.println("First Name: "+firstName+" Last Name: "+lastName);

System.out.println("Initials: "+initials);

System.out.println("Number of Characters in First Name: "+numCharactersFirst);

System.out.println("Number of Characters in Last Name: "+numCharactersLast);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote