Create a Java program that prompts the user to input a date in the format MM/DD/YYYY. The program should convert the entered date into its verbal representation according to the following guidelines: Print the English name of the month. Print the English representation of the day (e.g., first, second, ..., thirty-first). Display the English representation of the year, which consists of two parts: a century part ranging from 10 to 99, and a year part in the range from 0 to 99. Ensure to include a "zero" suffix when printing single-digit years. For example: The year 2003 should be represented as twenty zero three. The year 1992 should be represented as nineteen ninety-two. While this may seem like an unconventional programming task, it can be beneficial if you intend to use the resulting string with a text-to-voice converter to audibly communicate the result. Now use my code and rewrite the java program so that it includes what is missing in the assignment. import java.util.Scanner; public class TimeToWordsConversion { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Welcome to the Date to Words Converter!"); while (true) { System.out.print("Enter a date in format MM/DD/YYYY > "); String input = scanner.nextLine(); String[] parts = input.split("/"); int month = Integer.parseInt(parts[0]); int day = Integer.parseInt(parts[1]); int year = Integer.parseInt(parts[2]); if (month < 1 || month > 12) { System.out.println("Invalid month range. Please enter a valid month."); continue; } if (day < 1 || day > 31) { System.out.println("Invalid day range. Please enter a valid day."); continue; } if (year < 0) { System.out.println("Invalid year. Please enter a valid year."); continue; } String[] months = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String[] days = {"", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth", "twenty first", "twenty second", "twenty third", "twenty fourth", "twenty fifth", "twenty sixth", "twenty seventh", "twenty eighth", "twenty ninth", "thirtieth", "thirty first"}; String yearString = ""; int century = year / 100; int yearPart = year % 100; if (yearPart == 0) { yearString = "zero zero"; } else if (yearPart < 10) { yearString = "zero " + convertNumberToWord(yearPart); } else { yearString = convertNumberToWord(yearPart); } System.out.println(months[month] + " " + days[day] + ", " + convertNumberToWord(century) + " " + yearString); } } public static String convertNumberToWord(int number) { String[] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; String[] tens = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; String[] teens = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; if (number < 10) { return ones[number]; } else if (number < 20) { return teens[number - 10]; } else if (number < 100) { return tens[number / 10] + " " + ones[number % 10]; } else { return convertNumberToWord(number / 100) + " hundred " + convertNumberToWord(number % 100); } } }
Create a Java program that prompts the user to input a date in the format MM/DD/YYYY. The program should convert the entered date into its verbal representation according to the following guidelines:
- Print the English name of the month.
- Print the English representation of the day (e.g., first, second, ..., thirty-first).
- Display the English representation of the year, which consists of two parts: a century part ranging from 10 to 99, and a year part in the range from 0 to 99. Ensure to include a "zero" suffix when printing single-digit years.
For example:
- The year 2003 should be represented as twenty zero three.
- The year 1992 should be represented as nineteen ninety-two.
While this may seem like an unconventional
Now use my code and rewrite the java program so that it includes what is missing in the assignment.
import java.util.Scanner;
public class TimeToWordsConversion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Date to Words Converter!");
while (true) {
System.out.print("Enter a date in format MM/DD/YYYY > ");
String input = scanner.nextLine();
String[] parts = input.split("/");
int month = Integer.parseInt(parts[0]);
int day = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
if (month < 1 || month > 12) {
System.out.println("Invalid month range. Please enter a valid month.");
continue;
}
if (day < 1 || day > 31) {
System.out.println("Invalid day range. Please enter a valid day.");
continue;
}
if (year < 0) {
System.out.println("Invalid year. Please enter a valid year.");
continue;
}
String[] months = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String[] days = {"", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth", "twenty first", "twenty second", "twenty third", "twenty fourth", "twenty fifth", "twenty sixth", "twenty seventh", "twenty eighth", "twenty ninth", "thirtieth", "thirty first"};
String yearString = "";
int century = year / 100;
int yearPart = year % 100;
if (yearPart == 0) {
yearString = "zero zero";
} else if (yearPart < 10) {
yearString = "zero " + convertNumberToWord(yearPart);
} else {
yearString = convertNumberToWord(yearPart);
}
System.out.println(months[month] + " " + days[day] + ", " + convertNumberToWord(century) + " " + yearString);
}
}
public static String convertNumberToWord(int number) {
String[] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String[] tens = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String[] teens = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
if (number < 10) {
return ones[number];
} else if (number < 20) {
return teens[number - 10];
} else if (number < 100) {
return tens[number / 10] + " " + ones[number % 10];
} else {
return convertNumberToWord(number / 100) + " hundred " + convertNumberToWord(number % 100);
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps