-1

DateConversion program converts a date format entered by the user into another. This program works well the input format is mm/dd/yyyy but if user enteres 1/28/1999 instead of 01/28/1999, the program should still display January 28, 1999. What modifications can be done to do that. Any help will be appretiated! Code:

import java.util.Scanner; //Importing java Scanner
public class DateConversion { 
    public static void main (String[] args){ // Main method

        String enteredDate; // Declaring a string
        char answer = 'Y'; // Assigning a character value to variable answer
        String response; // Declaring a string for user's response
        Scanner keyboard = new Scanner (System.in); // Scanner object

        do { // A do-while loop used to check and convert the date format if user enters y otherwise terminate
        System.out.println("Please enter a date in mm/dd/yyyy format: "); // Asking for user input
        enteredDate = keyboard.nextLine(); // Taking the user's input

        // If, else if loops are used to check the months entered, convert and display them in another format
        // This is done by checking characters at various positions

        if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '1'){ 
            System.out.println("Display date in other format: ");
            System.out.println("January " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '2' ){
            System.out.println("Display date in other format: ");
            System.out.println("February " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '3' ){
            System.out.println("Display date in other format: ");
            System.out.println("March " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '4' ){
            System.out.println("Display date in other format: ");
            System.out.println("April " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '5' ){
            System.out.println("Display date in other format: ");
            System.out.println("May " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '6' ){
            System.out.println("Display date in other format: ");
            System.out.println("June " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '7' ){
            System.out.println("Display date in other format: ");
            System.out.println("July " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '8' ){
            System.out.println("Display date in other format: ");
            System.out.println("August " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '0' && enteredDate.charAt(1) == '9' ){
            System.out.println("Display date in other format: ");
            System.out.println("September " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '1' && enteredDate.charAt(1) == '0' ){
            System.out.println("Display date in other format: ");
            System.out.println("October " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '1' && enteredDate.charAt(1) == '1' ){
            System.out.println("Display date in other format: ");
            System.out.println("November " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        else if (enteredDate.charAt(0)== '1' && enteredDate.charAt(1) == '2' ){
            System.out.println("Display date in other format: ");
            System.out.println("December " +enteredDate.charAt(3)+""+enteredDate.charAt(4)+", "+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)+""+enteredDate.charAt(9));
        }
        System.out.println(); // Used for line break
        System.out.printf("Do you want to continue (y/n): "); // Asking the user if they want to continue

        response = keyboard.nextLine(); // User's response
        answer = response.charAt(0); // Getting the character at 0th position
        } while (Character.toUpperCase (answer) == 'Y'); // Checking if the character obtained is y
    }
}
  • You could also check if the charAt(0) == ‘1’ && charAt(1)==‘/‘. In my opinion it is not a beautiful solution but it should work and I think that the already existing code is more complicated than it should be, why not using SimpleDateFormat. But this is not related to your question therefore not arguing on that. – primef Mar 11 '18 at 15:55
  • Using standard classes for parsing and formatting dates is absolutely recommended. However, `SimpleDateFormat`, mentioned by @Fabrizio_P, is both long outdated and notoriously troublesome. For simple and clear code and easier debugging I recommend java.time, the modern Java date and time API. [The tutorial section on parsing and formatting is here.](https://docs.oracle.com/javase/tutorial/datetime/iso/format.html) – Ole V.V. Mar 11 '18 at 16:14
  • Is it a requirement that this be done without library classes? I could still think of an improvement or two to your code in that case. – Ole V.V. Mar 11 '18 at 16:17
  • It is a requirement to do this without the use of library classes. however, Any suggestions for improvement will be appretiated – John McDonald Mar 11 '18 at 16:45
  • your prompt says *Please enter a date in mm/dd/yyyy format*, if you detect any other format you should print an error message saying this format is required and to try again. Anything else is terrible defensive programming, and no matter what you read or told about about "being permissive/liberal on inputs, strict on output" they are wrong. You reject incorrect input, that is what makes a program robust and maintainable. Think about how robust a java source code file be if required things like `;` were allowed to be left out? Being able to leave out `{}` on `for/loops` tells you the answer. –  Mar 11 '18 at 17:58
  • Here is a great article about why ignore people that use [Postel's Principal,B*e conservative in what you do, be liberal in what you accept from others*](https://programmingisterrible.com/post/42215715657/postels-principle-is-a-bad-idea) as a religous mantra. It is just as much bad advice as [`null` is the billion dollar mistake](https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions). –  Mar 11 '18 at 18:16
  • @JarrodRoberson The sample run of the code is given and this is how the code should work. Actually this is an assignment. CODE: `Please enter a date in mm/dd/yyyy format: 12/30/2018 Display date in other format: December 30, 2018 Do you want to continue(y/n): y Please enter a date in mm/dd/yyyy format: 1/14/2018 Display date in other format: January 14, 2018 Do you want to continue(y/n): n` – John McDonald Mar 11 '18 at 18:29
  • 1
    @DakshSaini Providing more info is appreciated. When doing that, please [edit the question](https://stackoverflow.com/posts/49221815/edit) and add information in it so everything is in one place. In this case it will also allow prettier formatting. – Ole V.V. Mar 11 '18 at 19:01
  • `1/28/1999` does not fulfill the requirement *enter a date in mm/dd/yyyy format*. Trying to accept anything else is just wrong as it does not meet the format requirement. –  Mar 12 '18 at 03:48

1 Answers1

-1

If String.split is within bounds, use it for splitting the date string that the user has input into month, day-of-month and year. Then the month may come out as a String of either 1 or 01 if the user intended January, for example.

    String[] splitDate = enteredDate.split("/");

This will give you an array of three strings. You may want to check that the length of the array is 3 and issue an error message if not (if it’s shorter or longer). So you can access the month number string as splitDate[0], etc. Read up on arrays if you’re in doubt.

If String.split is not allowed, use String.substring for taking out the month as either 1 or 01 for January. This requires you know where the slash is. Use indexOf or just charAt to find out. Do similarly for day and year.

If you know a switch statement, switch on the month string you just got. You may have more than one case label before a case, for example:

    switch (monthNumberString) {
        case "01":
        case "1":
            System.out.print("January");
            break;
        case "02":
        case "2":
            System.out.print("February");
            break;
        // Etc. for other months
    }

(Another option would be a Map for holding the mapping from numeric string to month name, but I would suppose that’s out of bounds too.)

If you don’t know switch (or is not allowed to use it), you may still use the month substring in your if statements:

    if (monthNumberString.equals("01") || monthNumberString.equals("1"))

Minor sugggestions:

  • Use an else part in the end (or a default case if using a switch statement) to print a message about an unrecognized month.
  • Don’t declare a variable until you need it. In your program enteredDate and response can be declared inside the do loop where you first use them to make it easier to find the way around in the code.
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thank-you for your suggestion. Actually used else if statements : `else if (enteredDate.charAt(0)=='1' && enteredDate.charAt(1)=='/'){ System.out.println("January " +enteredDate.charAt(2)+""+enteredDate.charAt(3)+", "+enteredDate.charAt(5)+""+enteredDate.charAt(6)+""+enteredDate.charAt(7)+""+enteredDate.charAt(8)); ` and this works well but I want to make the length of the code short. Is there any other way of doing this? – John McDonald Mar 11 '18 at 18:21
  • @DakshSaini Glad you found a working solution. I still believe my suggestions in the answer may give you not only shorter code, but also code that is clearer and easier to read. In real life this matters. – Ole V.V. Mar 11 '18 at 20:15
  • Thank-you for the suggestion. However, I did'nt get how to get the substring from the string input for month. Like using split will make 3 tokens, so how to get each token and then work on them? – John McDonald Mar 11 '18 at 21:33
  • See my edit. @DakshSaini – Ole V.V. Mar 12 '18 at 08:50
  • My answer has received 2 downvotes and a delete vote. I’m curious as to whether it’s because the voters think (1) I shouldn’t answer a poor question like this (2) I should recommend `java.time` even though the asker said “It is a requirement to do this without the use of library classes”, (3) I should recommend insisting on two-digit input even though the asker is required by the assignment to accept 1 digit or (4) there’s something poor or bad about the answer. I should love to be enlightened. – Ole V.V. Jun 04 '18 at 10:36