Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Fix this Java code and make it run sucessfulin a Java program. import java.util.Scanner; public class timetowordsconversaion { public static void main ( String [

Fix this Java code and make it run sucessfulin a Java program.
import java.util.Scanner;
public class timetowordsconversaion {
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;
}
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 "";
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students explore these related Databases questions