Question

Write a java program Problem 4 involves a date in the form of mm/dd/yyyy. You must...

Write a java program

Problem 4

involves a date in the form of mm/dd/yyyy. You must write a java program that checks if a date is valid or not , make sure that the program tells you where the error is in the program if there is one . I want you to work with characters and the switch statement, and write out a reason for each incorrect input value. .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

================================ java code for validation of the given format is===================

import java.util.Scanner;

public class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
// extractin out day month and year part separately
System.out.println ("Enter the date format{mm/dd/yyyy}:");
String date = sc.next ();
String dateArray[] = date.split ("/");
int month = 0;
int day = 0;
int year = 0;
//looping for validating each part
for (int i = 0; i < 3; i++)
{

   switch (i)
   {
   case 0:
   // month lenth validating
   if (dateArray[0].length () != 2)
   {
       System.out.println ("Error in date-month part length");
       System.exit (0);

   }

   try
   {

   int first = Character.getNumericValue (dateArray[0].charAt (0));
   int second =
       Character.getNumericValue (dateArray[0].charAt (1));
   month = first * 10 + second;

   } catch (Exception e)
   {
   // month correct digit validating
   System.out.
       println ("month should be a two digit number not character");
   System.exit (0);
   }
   break;
   case 1:
   //day length validating
   if (dateArray[1].length () != 2)
   {
       System.out.println ("Error in date-day part length!");
       System.exit (0);

   }

   try
   {

   int first = Character.getNumericValue (dateArray[1].charAt (0));
   int second =
       Character.getNumericValue (dateArray[1].charAt (1));
   day = first * 10 + second;
   //day high range validating
   int highRange = 0;
   switch (month)
       {
       case 1:
       highRange = 31;
       break;
       case 2:
       highRange = 28;
       break;
       case 3:
       highRange = 31;
       break;
       case 4:
       highRange = 30;
       break;
       case 5:
       highRange = 31;
       break;
       case 6:
       highRange = 30;
       break;
       case 7:
       highRange = 31;
       break;
       case 8:
       highRange = 31;
       break;
       case 9:
       highRange = 30;
       break;
       case 10:
       highRange = 31;
       break;
       case 11:
       highRange = 30;
       break;
       case 12:
       highRange = 31;
       break;
       default:
       System.out.println ("Month should be between 1-12");
       System.exit (0);

       }

   //validating day highRange
   if (day < 0 || day > highRange)
       {
       System.out.println ("Error in day range --");
       }
   else
       {
       System.out.println ("Day is correct!--");
       }

   }
   catch (Exception e)
   {
   //validating if day is character or digit
   System.out.
       println ("day should be a two digit number not character");
   System.exit (0);
   }
   break;
   case 2:
   //year length validating
   if (dateArray[2].length () != 4)
   {
       System.out.println ("Error in date-year part length!");
       System.exit (0);

   }
   try
   {


   int first = Character.getNumericValue (dateArray[2].charAt (0));
   int second =
       Character.getNumericValue (dateArray[2].charAt (1));

   int third = Character.getNumericValue (dateArray[2].charAt (2));
   int fourth =
       Character.getNumericValue (dateArray[2].charAt (3));
   //leap year validating
   year = first * 1000 + second * 100 + third * 10 + fourth;
   if (year % 4 == 0 || (year % 100 == 0 && year % 400 == 0))
       {
       if (month == 2 && day > 29)
       {
       System.out.println ("In leap year february is of 29--");
       System.exit (0);
       }
       else
       {
       System.out.println ("Month part is correct!");
       }

       }
   else
       {
       System.out.println ("Month part is Correct!");
       }

   System.out.
       println ("Yearis correct \n Date is in correct format!");

   }
   catch (Exception e)
   {
   System.out.
       println ("year should be a number not characters or string!");
   System.exit (0);

   }

   break;
   }
}
}
}


-------------------------------------------------------------------------------------------------------------------------------------------------------

Screenshots of the code and the output:

------------------------------------------------------------------------------------------------------------------------------------------------------

================================ end=========================================

*************** please like the post it motivates me****************************************************

Add a comment
Know the answer?
Add Answer to:
Write a java program Problem 4 involves a date in the form of mm/dd/yyyy. You must...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that accepts a date from the user in the form mm/dd/yyyy

    # C PROGRAMMING LANGUAGEWrite a program that accepts a date from the user in the form mm/dd/yyyy and then dis- plays it in the form month dd, yyyy, where month is the name of the month: Enter a date (mm/dd/yyyy): 2/17/2011 You entered the date February 17, 2011 Store the month names in an array that contains pointers to strings.

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

    Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program should use a single input statement to accept the date, storing each piece of the date into an appropriate variable. Demonstrate that the input was obtained correctly by outputting the month, day, and year to the screen. Sample output: Enter (date (mm/dd/yyyy): 02/08/2011 Month entered: 2 Day entered: 8 Year entered: 2011 Challenge: Although the user entered 02, C++ drops the 0 as insignificant...

  • Write a Java console application that reads a string from the keyboard and tests whether it...

    Write a Java console application that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is NOT valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). You can use: String monthPart = inputDate.substring(0, 2); int month =...

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • 3. Date Printer Write a program that reads string from the user containing a date in...

    3. Date Printer Write a program that reads string from the user containing a date in the form mm/dd/yyyy. It should print the date in the form March 12, 2014 . ( it is starting out with python third edition ) Can anyone please do it by python program .

  • Write a program that reads a string from the user containing a date in the form...

    Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018   I am asking for help in basic python please

  • JAVA CODE Beginner. please use if, else if or switch statements, but don't use arrays, please  ...

    JAVA CODE Beginner. please use if, else if or switch statements, but don't use arrays, please   Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12...

  • IN PYTHON, Write a program that reads a string from the user containing a date in...

    IN PYTHON, Write a program that reads a string from the user containing a date in the form mm/dd/ yyyy. It should print the date in the form April 12, 2017. Hint: Get the "mm" from the user entered date "mm/dd/yyyy", then convert the "mm" into a month number. You may use an list month_list = ['January', 'February','March','April', 'May','June', 'July','August', 'September', 'October', 'November', 'December']. Use the month number you got from "mm" to get the month name.

  • Write a C++ program that determines the user's age after the   user enters both the current...

    Write a C++ program that determines the user's age after the   user enters both the current date and hisher birthdate as 3   integers:                yyyy mm dd                                                                                             Define a class which is named DateType and will be used to     declare 2 objects: "birthday" and "today". The class will have a function "output" which will display the date in conventional form (like it is above next to Date Assigned: Month dd, yyyy and it will have a second function named     “input”...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT