Question

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 (January is 1). The 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. February has 28 days except for leap years when it has 29. The remaining months all have 31 days each. A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class ValidateDate {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a date in the format mm/dd/yyyy");
        String date = in.nextLine();
        int index = date.indexOf('/');
        if(index == -1) {
            System.out.println(date + " is NOT a valid date, because date is not formatted correctly");
        } else {
            try {
                int month = Integer.parseInt(date.substring(0, index));
                int nextIndex = date.indexOf('/', index+1);
                if(nextIndex == -1) {
                    System.out.println(date + " is NOT a valid date, because date is not formatted correctly");
                } else {
                    int day = Integer.parseInt(date.substring(index + 1, nextIndex));
                    int year = Integer.parseInt(date.substring(nextIndex + 1));
                    boolean isLeap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
                    if (month >= 1 && month <= 12) {
                        if (day <= 0) {
                            System.out.println(date + " is NOT a valid date, because day " + day + " is invalid");
                        } else {
                            if (day <= 30 && (month == 4 || month == 6 || month == 9 || month == 11)) {
                                System.out.println(date + " is a valid date!");
                            } else if (day <= 29 && month == 2 && isLeap) {
                                System.out.println(date + " is a valid date!");
                            } else if (day <= 28 && month == 2) {
                                System.out.println(date + " is a valid date!");
                            } else if (day <= 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
                                System.out.println(date + " is a valid date!");
                            } else {
                                System.out.println(date + " is NOT a valid date, because day " + day + " is invalid for month " + month);
                            }
                        }
                    } else {
                        System.out.println(date + " is NOT a valid date, because month " + month + " is invalid");
                    }
                }
            } catch (Exception e) {
                System.out.println(date + " is NOT a valid date, because date is not formatted correctly");
            }
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
JAVA CODE Beginner. please use if, else if or switch statements, but don't use arrays, please  ...
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 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 =...

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

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Extend this date validation program, so that it checks for valid leap years (in which the...

    Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • can you please help with this problem? [L8-2] (date_parse.java) Complete the provided code, finishing the function...

    can you please help with this problem? [L8-2] (date_parse.java) Complete the provided code, finishing the function parse_date(dstring). When called, pass dstring as mm/dd/yyyy, where mm, dd, and yyyy are integers. The function should split dstring into individual strings, convert each to integer month, day, and year, then return the tuple (month,day,year). The provided main() reads date as a str value from the user, calls parse_date(date), then prints out the returned result. Note: the starting code is here: def parse_date(dstring): '''...

  • Need help writing beginner C# program, I will make sure to provide feedback to whoever can...

    Need help writing beginner C# program, I will make sure to provide feedback to whoever can help me figure it out! No public or global variables should be used. You need to consider passing arguments between the methods according to the ways described in the lecture. i.e. all variables should be declared inside the methods and passed to other methods by value/ref/out as needed Description: We want to design a Date class to represent a date using three integer numbers...

  • C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

    C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...

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