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.

Try debugging your code by adding printouts of all the variables you are using to see where you are going wrong. For example:
System.out.println("dte=" + dte + "<");
System.out.println("index1=" + index1);

Try debugging your code by adding printouts of all the variables you are using to see where you are going wrong. For example:
System.out.println("dte=" + dte + "<");
System.out.println("index1=" + index1);

I still don't get what I'm doing wrong. I did what you gave me and here is what I got:

Enter date to be converted: APrIl 24, 2001
dte=APrIl 24, 2001<
index1=5

Is there a problem with the index or is there something else I need to add in there?

Sorry, I forget to tell you to look at the values that are printed and see if they are what you need to do the substrings.
YOU left off printing out the values of the rest of the variables in your program that are used to create the output.

Now you need to take a piece of paper and a pencil and write out the String and play computer: do the indexOf methods and see what the values of the indexes should be for the resultant Strings to be what you want them to be.

Sorry, I forget to tell you to look at the values that are printed and see if they are what you need to do the substrings.
YOU left off printing out the values of the rest of the variables in your program that are used to create the output.

Now you need to take a piece of paper and a pencil and write out the String and play computer: do the indexOf methods and see what the values of the indexes should be for the resultant Strings to be what you want them to be.

Hey, sorry, I forgot to post what I did wrong lol. Anyways, it turns out that I did the indexes wrong for dte2. This is what I did here:

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 and lowercasing on the other ones */
    	String firstLetter = dte.substring(0,1);
    	String otherLetters = dte.substring(1);
    	String dte0 = firstLetter.trim().toUpperCase() + otherLetters.trim().toLowerCase();

		// Gives the proper out of where month, day, and year should be found
 		String dte1 = dte0.trim().substring(0, index1).trim();
    	String dte2 = dte.trim().substring(index1 + 1, index2).trim();
    	String dte3 = dte.trim().substring(index2 + 1).trim();

		// Gives the output of an accurate date without any extra spaces
    	System.out.println("Converted Date: " + dte2 + " " + dte1 + " " + dte3);
    }
}

Also, for index2, I forgot to put a comma in there which is suppose to show where the day is suppose to end at. For the rest, I've added trim to dte1, dte2, and dte3 which will take out any extra spaces. So now, everything should be working fine.
Thanks for the advice, that was very useful. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.