In: Computer Science
In.java
Write down a program that asks the user for their full name given in the format first last. The program will do the necessary processing and print the name in a table with 3 columns: Last, First, Initials. Requirements/Specifications...
as a Sample run Please enter your first and last name: Tom Cruise ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |Cruise |Tom |T.C.| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Summary-
The input is received by a prompt from the user in the form of Strings using two variables first and last for this purpose.
The output is generated as above as given in the question using the "printf" and "println" Statements.
By using the printf statement, left alignment of 15 spaces is given for the first name and last name in the output.
For the printing of the initials , charAt() statement is used.
Code-
import java.util.*;
public class Bts {
public static void main(String[] args) {
//Declaring variables for first and last names
String first,last;
//Scanner class to receive the inputs
Scanner sc=new Scanner(System.in);
//Prompting the user to enter an input
System.out.printf("Please enter your first and last
name: ");
first=sc.next();
last=sc.next();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.printf("|");
System.out.printf("%-15s",last);
System.out.printf("|");
System.out.printf("%-15s",first);
System.out.printf("|");
System.out.printf(first.charAt(0)+"."+last.charAt(0)+".");
System.out.printf("|");
System.out.println();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
Output-