Hi, I'm having a small problem with the program that I'm doing. Here is the instructions for it:
Write a program named ConvertDate that converts a date entered by the user into another form. The user’s input will have the form
month day, year
The month will be one of the words January, February, etc.. The letters in the month may be lowercase, uppercase or any mixture of the two. The day is a one- or two-digit number. The year is any number of digits.
There may be 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 month 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 on the month must be uppercase, and the remaining letters must be lowercase.
The following example shows what the user will see on the screen:
Enter date to be converted: apRiL 28, 2003
Converted date: 28 April 2003
Here is my code:
import jpb.*;
public class ConvertDate
{
public static void main(String[] args)
{
// Inputs the month, day, and year
SimpleIO.prompt("Enter date to be converted: ");
String date = SimpleIO.readLine();
// Trims the extra spaces
String dte = date.trim();
// Creates the minimum space between the characters
int index1 = dte.indexOf(" ");
int index2 = dte.lastIndexOf(" ");
/* This is where the letters of the month are going to be store at in the output as well as capitalizing the first letter
of the month */
String firstLetter = dte.substring(0,1);
String otherLetters = dte.substring(1);
String dte0 = firstLetter.trim().toUpperCase() + otherLetters.trim().toLowerCase();
// Trims the extra spaces from the date
String dte1 = dte0.trim().substring(0, index1);
String dte2 = dte.trim().substring(index1 + 1, index1 + 4);
String dte3 = dte.trim().substring(index2 + 1);
// Gives the output of an accurate date without any extra spaces
System.out.println("Converted Date: " + dte2 + " " + dte1 + " " + dte3);
}
}
So far, both my month and year are alright. No extra spaces were there and they're in the right position. However, there a problem with the way the day is being printed out.
Here is the output that I'm getting:
Enter date to be converted: May 13, 2009
Converted Date: 13, May 2009
I'm not suppose to have a comma in there. This is without any extra spaces. With a couple of extra spaces, it won't the date will be missing a number sometimes or it will not be there. How do I fix this problem and make it print correctly?
P.S. Again, both month and year are printing fine.