write a java program named ConvertDate that converts a date entered by the user
ID: 3774701 • Letter: W
Question
write a java program named ConvertDate that converts a date entered by the user into another form. The users inout will have the form
month day, year.
The month will be one of the words January, February.. the letters in the month maybe lowercase, uppercase, or any mixture of the two. the day is one or two digit number. the year is any number of digits. there maybe any number of spaces (1) before the month, (2) between the month and the day (3) between the day and the year, and (4) after the year. You may assume that there is at least one space between the mont and day and between the day and year. You may also assume that there is a comma after the day. The converted date must have the form
day month year
with one space between the day and month and between the month and year. the first letter of the month must be uppercase and the remaining letters must be lowercase. the following examples show what the the screen"
enter date to be converted: april 28, 2003
converted date: 28 april 2003
Hint: use the index Od and lastIndexOf methods to locate the month, day and year within the string entered by the user, and then use the substring method to extract them. ***Please comment what each line of code does. Thank you***
Explanation / Answer
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
String date;
Scanner input=new Scanner(System.in);
System.out.print("enter date to be converted:");
date=input.nextLine(); //storing user entered date
//using trim method to trim the leading and trailling spaces of the string date
String date1= date.trim();
//using replace to remove , from the string
String date2=date1.replace(","," ");
//converting first character to uppercase and remaining into lowercase
String date3=date2.substring(0,1).toUpperCase() + date2.substring(1).toLowerCase();
//using split method to remove the middle spaces and storing the splitted words into string array
String[] words=date3.split("\s+");
//printing the string in the required format
System.out.println("Converted date:"+words[1]+" "+words[0]+" "+words[2]);
}
}
/*******OUTPUT***********
enter date to be converted: aPRil 28, 2003
Converted date:28 April 2003
*******OUTPUT***********/
/* Note:this code has been tested on eclipse,please do ask in case of any doubt */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.