We need to write a program to report information about a President of the United
ID: 3808232 • Letter: W
Question
We need to write a program to report information about a President of the United States. We will need to read in information from the keyboard which contain the needed data for this report. The input consists of a line of text. The line of text will contain the data for a President in the form of multiple fields of information. The fields are separated by semicolons ( ; ) and may contain optional white space before and/or after the semicolons. The line contains four (4) fields:
The number of the President (see examples below).
The name of the President in the format: lastName, firstName [middleInitials]
The brackets around the middleInitials are meta-characters and serve to indicate that the middle initial is optional and not required. The brackets are not part of the format. However, the comma between the last and the first name is required syntax (see examples below).
The first year of the President’s term in office as a four (4) digit numeric value.
The number of full years that the President served. In the case of the current President, since this is the middle of his first year, the field will contain zero (0).
Example input values:
40 ; Reagan, Ronald W. ; 1981 ; 8
The output of the program will be a line in the following format:
firstName [middleInitials] lastName, numWithOrdSuffix President of the United States, from firstYear to lastYear.
As above the brackets indicate an optional field.
NumWithOrdSuffix is the number of the president followed by the ordinality suffix:
40th 41st 42nd 43rd 44th… (note the comma after the last name).
lastYear will need to be computed based on the firstYear and the number of full years served.
Example output values:
Ronald W. Reagan, 40th President of the United States, from 1981 to 1989.
Explanation / Answer
import java.util.Scanner;
public class PresidentInfo {
public static void main(String[] args) {
System.out.println("Enter the Information: ");
//reading the line from keyboard
Scanner sc = new Scanner(System.in);
String presidentInfo = sc.nextLine();
//splitted the string using regex ";"
String arr[] = presidentInfo.split(";");
//getting the Full name and splitting the string again using regex ","
String name[] = arr[1].trim().split(",");
//first Name of president from the name array
String firstName = name[1].trim();
//last Name of president from the name array
String lastName = name[0].trim();
//Number of president
String presidentNo = arr[0].trim();
//First Year of Term
String firstYearofTerm = arr[2].trim();
//Total years served
String totalYearofService = arr[3].trim();
//calculating the last year of term
int lastYearOfTerm = Integer.parseInt(firstYearofTerm) + Integer.parseInt(totalYearofService);
//Finding out the suffix to be added
String suffix = null;
if (presidentNo.endsWith("1")) {
suffix = "st";
} else if (presidentNo.endsWith("2")) {
suffix = "nd";
} else if (presidentNo.endsWith("3")) {
suffix = "rd";
} else {
suffix = "th";
}
//Printing in the format as specified
System.out.println(firstName +" "+ lastName + ", " + presidentNo + suffix + " President of the United States, from "+firstYearofTerm+" to "+lastYearOfTerm+".");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.