Question

In: Computer Science

Write a program that prompts the user to enter a date in the format mm/dd/yyyy where...

Write a program that prompts the user to enter a date in the format mm/dd/yyyy
where mm is the month , dd is the day and yyyy is a 4 digit year
Check if the date is valid by seeing month is between 1 and 12, day is between 1 to 31 and year is between 1800 to 3000. Also check that if month is 2, day is between 1 to 29
If the date is valid then display the date back in following format dd month year. Use of SimpleDateFormat Java Class is NOT allowed to do this assignment. You need to use String functions from Chapter 4 to do this assignment.

Following is a sample run:
Enter a date in the format mm/dd/yyyy:
02/23/1999
23 Feb 1999

Following is another sample run:

Enter a date in the format mm/dd/yyyy:
02021999
Input String Length should be 10

Following is another sample run:

Enter a date in the format mm/dd/yyyy:
02-23-2000
Invalid Date Format

Following is another sample run:

Enter a date in the format mm/dd/yyyy:
23/02/2000
Invalid Date

Solutions

Expert Solution

import java.util.Scanner;

public class StringDate {
   public static void main(String[] args) {
       String arr[] = { " ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
               "Oct", "Nov", "Dec" };
       System.out.println("Enter a date in the format ");
       Scanner sc = new Scanner(System.in);
       String date = sc.next();
       //checking if it has invalid length
       if (date.length() < 10) {
           System.out.println("Input String Length should be 10");
           return;
       }
       //splitting the string to get day,month,year
       String splt[] = date.split("/");
       int day = Integer.parseInt(splt[1]);
       int month = Integer.parseInt(splt[0]);
       int year = Integer.parseInt(splt[2]);
       //checking if it valid year and month
       if (year < 1800 || year > 3000 || month < 1 || month > 12) {
           System.out.println("Invalid Date format");
           return;
       }
       if(!(day>=1 && day<getDays(month))){
           System.out.println("Invalid Date format");
           return;
       }
       System.out.println(day+" "+arr[month]+" "+year);

   }
   private static int getDays(int mm) {
       int res = -1;
       switch (mm) {
       case 1:
           res = 31;
           break;
       case 2:
               res = 29;
           break;
       case 3:
           res = 31;
           break;
       case 4:
           res = 30;
           break;
       case 5:
           res = 31;
           break;
       case 6:
           res = 30;
           break;
       case 7:
           res = 31;
           break;
       case 8:
           res = 31;
           break;
       case 9:
           res = 30;
           break;
       case 10:
           res = 31;
           break;
       case 11:
           res = 30;
           break;
       case 12:
           res = 31;
           break;

       }
       return res;
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If...
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If the age is greater than 21 print "welcome," and if the age is less than 21 print "sorry." Use input validation to make sure the birthdate was entered correctly.
Write a program that prompts the user to enter a person’s date of birth in numeric...
Write a program that prompts the user to enter a person’s date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Follow similar conventions for the invalid values of month and year. (Note that your program...
How to tokenize a string date of format        dd/mm/yyyy into day,month and year without using built...
How to tokenize a string date of format        dd/mm/yyyy into day,month and year without using built in function of strok() or any other built in function in C++ (without classes). Kindly help Please .
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format,...
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format, age, and department. In the class, include getter and setter methods to get and set the values of those variables. Author a data entry webform using HTML text input boxes and a submit button. When the user clicks on the submit button, a "User" class is instantiated and the new User object's fields are populated. The HTML input boxes correspond to the field in...
Write program that prompts the user to enter a person's date of birth in numeric form...
Write program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle...
Write a java program that prompts user to enter his name and KSU ID using format...
Write a java program that prompts user to enter his name and KSU ID using format name-ID as one value, his gender as char, and his GPA out of 5. Then your program should do the following: Print whether the ID is valid or not, where a valid ID should be of length = 9, and should start with 4. Calculate the GPA out of 100 which is calculated as follows GPA/5 * 100. Update the name after converting the...
Create a program that when run, prompts the user to enter a city name, a date,...
Create a program that when run, prompts the user to enter a city name, a date, the minimum temperature and the maximum temperature. Calculate the difference between the maximum temperature and the minimum temperature. Calculate the average temperature based on the minimum and maximum temperature. Print out the following: City name today's Date minimum temperature maximum temperature average temperature difference between minimum and maximum The following is a sample run, user input is shown in bold underline. Enter City Name:...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT