Question
4.8 Lab Warmup: String Input and Formatting The template reads a person's first and last name into String variables. Edit the program template to: Create
4.8 Lab Warmup: String Input and Formatting
The template reads a person's first and last name into String variables. Edit the program template to: Create the variables for a second person's first and last names. Read the second person's first name and last name into the separate string variables. Assumptions: The first and last names contain no spaces and are no longer than 12 characters each. Then use printf to display the names, formatted exactly as follows, so the commas line up. Since none of the last names will be longer than 12 characters, display 12 characters before the comma. Hints: Use the width specifier to line up the last names. Use the precision specifier to display only the first letter of each person's name Output a newline after each name is displayed. Example:
Enter one person's first and last name: Paul Revere Enter a second person's first and last name: Martha Washington Revere, P Washington, W
import java.util.Scanner;
public class StringFormatting { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String firstName1 = ""; String lastName1 = ""; // FIXME: Create variables System.out.println("Enter a person's first and last name:"); firstName1 = keyboard.next(); lastName1 = keyboard.next(); // FIXME: Prompt for and read second person's name System.out.println(); // FIXME: Display formatted names return; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started