Language is Java
Name format
Many documents use a specific format for a person's name. Write a program whose input is:
firstName middleName lastName
and whose output is:
lastName, firstInitial.middleInitial.
Ex: If the input is:
Pat Silly Doe
the output is:
Doe, P.S.
If the input has the form:
firstName lastName
the output is:
lastName, firstInitial.
Ex: If the input is:
Julia Clark
the output is:
Clark, J.
import java.util.Scanner; class LabProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] words = s.split(" "); String firstName = words[0]; String middleName = words[1]; if (words.length == 2) System.out.println(middleName + ", " + firstName.charAt(0) + "."); else { String lastName = words[2]; System.out.println(lastName + ", " + firstName.charAt(0) + "." + middleName.charAt(0) + "."); } } }
Get Answers For Free
Most questions answered within 1 hours.