Question

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 = Integer.parseInt(monthPart);
to get the month value.

A valid day value dd must be from 1 to a value that is appropriate for the given month. September, April, June, and November each have 30 days. Assume February has 28 days. All other months have 31 days.
The year can be any 4 digit year.

***Be sure to use a switch statement at least once (perhaps for checking the valid months)***

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

//ValidDate.java

import java.util.Scanner;

public class ValidDate {

   public static void main(String[] args) {
      
   //Array of months
   String months[] = {
       "1,January,31","2,Feburary,28", "3,March,31","4,April,30"
       ,"5,May,31","6,June,30","7,July,31","8,August,31",
       "9,September,30","10,October,31","11,November,30"
       ,"12,December,31"
   };
      
       // Reading date as String with mm/dd/yyyy
       String sDate;
       Scanner s = new Scanner(System.in);
       System.out.println("Enter the date:");
       sDate = s.next();
      
       String f[] = sDate.split("/"); // Splitting date into 3 parts
       if(f.length<3){
           System.out.println("Invalid Input... Give as mm/dd/yyyy");
       }else {
       int day = Integer.parseInt(f[1]); // Day 1-30 or 31 or 28
       int month = Integer.parseInt(f[0]); // Months 1-12
       String year = f[2]; // Year 4 digit number
       String x="";
       boolean flag = false;
       StringBuffer sb = new StringBuffer();
       switch(month){ // Passing month ino switch
       case 1:
       case 2:
       case 3:
       case 4:
       case 5:
       case 6:
       case 7:
       case 8:
       case 9:
       case 10:
       case 11:
       case 12:               
           x = months[month-1]; // getting value from array
           String dd[] = x.split(","); // split using comma
           if(day<=Integer.parseInt(dd[2])){ // Validating Day
               if(year.length()==4){ // Validating year
                   flag = true;
               } else{
                   sb.append("Invalid Year.. year should be 1000-9999");
               }
           }else {
               sb.append("Invalid Number of days for Month "+dd[1]);
           }
           break;
       default : sb.append("Invalid Month, should be between 1-12"); // validating Month
       }
      
       if(flag){
           System.out.println("Valid Date Format:");
       } else {
           System.out.println("Invalid Date format");
           System.out.println(sb.toString());
       }
   }
      
   }

}

Output:

Enter the date:
12/2016
Invalid Input... Give as mm/dd/yyyy

Enter the date:
12/12/2018
Valid Date Format:

Enter the date:
02/29/2017
Invalid Date format
Invalid Number of days for Month Feburary

Enter the date:
14/22/2016
Invalid Date format
Invalid Month, should be between 1-12

Enter the date:
12/12/201
Invalid Date format
Invalid Year.. year should be 1000-9999

Add a comment
Know the answer?
Add Answer to:
Write a Java console application that reads a string from the keyboard and tests whether it...
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
  • 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...

  • 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...

  • 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...

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • 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.

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

    Python 3. Date Printer 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 3. Date Printer 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

  • 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

  • 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 an application in java that reads in a five-digit integer and determines whether it is...

    Write an application in java that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long, display an error message and allow the user to enter a new value. Tips: 1. Determine the number of digits in the value input by the user , i.e. prompt the user to enter the number of digits of the number. Use a while loop to determine whether the user input contains the proper...

  • Write a java application, Date, that reads an 8-digit integer value (indicating a date) from the...

    Write a java application, Date, that reads an 8-digit integer value (indicating a date) from the keyboard; the first two digits indicate the month, the second two digits represent the day and the last 4 digits represent the year. After calculating the month, the application will implement a switch statement to output the month. After displaying the date, the application will implement a nested if else statement to output the appropriate century. i.e Prev17th century, 18th century, 19th century, 20th...

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