Write a Java program call FancyMyName which asks you to write a program that tes
ID: 3774437 • Letter: W
Question
Write a Java 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. ***Could you please comment what each line of code does***
A sample run of the program should look something like this:
Please enter your first name and last name, separated by a space?
You entered the name: Bill Gates
Your first name is Bill: has 4 characters
Your last name is Gates: has 5 characters
Your initials are: BG
Explanation / Answer
Hi, Please find implementation.Please let me know in case of any issue.
import java.util.Scanner;
public class FancyMyName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// getting name
System.out.print("You entered the name: ");
String name = sc.nextLine();
// getting index of space
int index = name.indexOf(' ');
// splitting name in firstName and lastName
String fName = name.substring(0, index);
String lName = name.substring(index+1);
System.out.println("Your first name is "+fName+": has "+fName.length()+" characters");
System.out.println("Your last name is "+lName+": has "+lName.length()+" characters");
System.out.println("Your initials are: "+
Character.toUpperCase(fName.charAt(0)) + Character.toUpperCase(lName.charAt(0)));
}
}
/*
Sample run:
You entered the name: Pravesh Kumar
Your first name is Pravesh: has 7 characters
Your last name is Kumar: has 5 characters
Your initials are: PK
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.