Write a java program PlayWithName.java that should print the output as shown bas
ID: 3670058 • Letter: W
Question
Write a java program PlayWithName.java that should print the output as shown based on the name specification given for the code. The name will be entered by the user from the keyboard. Compute the output values using methods available to objects in the class String. Please don’t code in the output Strings. How would this code be written in import java.util.Scanner? Thank you.
What is your first name: John
What is your middle name: Omega
What is your last name: Phi
Name = John Omega Phi
Initials = JOP
Short name = John O Phi
Shorter name = J O Phi
UpperCase name = JOHN OMEGA PHI
Note: s.substring(0,1) returns a String which is the first character of the String s.
Explanation / Answer
import java.util.*; import java.io.*; public class NameSplit { publicstaticvoid main(String[] args) { String fullName; System.out.println(" Enter the full name : "); Scanner s = new Scanner ( System.in ); fullName = s.readLine(); String[] tokens = fullName.split(" "); String firstName =""; String middleName =""; String lastName =""; if(tokens.length >0) { firstName = tokens[0]; middleName = tokens.length >2? getMiddleName(tokens):""; lastName = tokens[tokens.length -1]; } System.out.println(firstName); System.out.println(middleName); System.out.println(lastName); } public static String getMiddleName(String[] middleName) { StringBuilder builder =newStringBuilder(); for(int i =1; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.