This program will ask the user to enter their first name and

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. Separate 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() 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: You entered the name: Louis Henry Your first name is Louis: has 5 characters Your last name is Henry: has 5 characters Your initials are: LH

Solution

class Name
{
public static void main(String args[])
{
String name, firstName,lastName;
int blankIndex,fchar,lchar;

System.out.println(\"Enter your name having space between first name and last name\");
Scanner in = new Scanner(System.in);
name= in.nextLine();
blankIndex = name.indexOf(\' \');
firstName = name.substring(0,blankIndex-1);
lastName = name.substring(blankIndex+1, name.length()-1);

fchar = firstName.length();
lchar = lastName.lenght();
System.out.println(\"You entered the name\"+name);   
System.out.println(\"Your firstname is\"+firstName+\"has\"+fchar+\"characters\" );
System.out.println(\"Your lastname is\"+lastName+\"has\"+lchar+\"characters\" );
}
}

 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. Separate the input

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site