Question
LANGUAGE IS JAVA import java.util.Scanner; public class DateConverter { public static void main(String [] args) { Scanner scnr = new Scanner(System.in); int dayNumber; int monthNumber;
LANGUAGE IS JAVA
import java.util.Scanner;
public class DateConverter {
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
int dayNumber;
int monthNumber;
int date;
int year;
String monthName = "", dayName="";
boolean valid = false;
System.out.println("Enter 4 integers representing dayNumber monthNumber date year:");
dayNumber = scnr.nextInt();
monthNumber = scnr.nextInt();
date = scnr.nextInt();
year = scnr.nextInt();
if ((dayNumber < 1) || (dayNumber > 7)){
System.out.println("Invalid day number: " + dayNumber + ", please enter a number from 1..7.");
}
else if ((monthNumber < 1) || (monthNumber > 12)) {
System.out.println("Invalid month number: " + monthNumber + ", please enter a number from 1..12.");
}
else if ((date < 1) || (date > 31)) {
System.out.println("Invalid date number: " + date + ", please enter a number from 1..31.");
}
switch (monthNumber) {
case 1: monthName="January";
break;
case 2: monthName="February";
break;
case 3: monthName="March";
break;
case 4: monthName="April";
break;
case 5: monthName="May";
break;
case 6: monthName="June";
break;
case 7: monthName="July";
break;
case 8: monthName="August";
break;
case 9: monthName="September";
break;
case 10: monthName="October";
break;
case 11: monthName="November";
break;
case 12: monthName="December";
break;
}
switch (dayNumber) {
case 1: dayName="Sunday";
break;
case 2: dayName="Monday";
break;
case 3: dayName="Tuesday";
break;
case 4: dayName="Wednesday";
break;
case 5: dayName="Thursday";
break;
case 6: dayName="Friday";
break;
case 7: dayName="Saturday";
break;
}
switch(monthNumber)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (date >= 31) {
System.out.println("Invalid date: " + monthName + ", does not have " + date + " days, please enter a valid date.");
}
else {
valid = true;
}
break;
case 4:
case 6:
case 9:
case 11:
if (date >= 30) {
System.out.println("Invalid date: " + monthName + ", does not have date days, please enter a valid date.");
}
else {
valid = true;
}
break;
case 2:
if( ((year%400) == 0) || ((((year%4) == 0)) && ((year%100) != 0)) )
{
if (date == 29) {
valid = true;
}
else {
System.out.println("Invalid date: " + year + " is a leap year, February does not have " + date + " days, please enter a valid date.");
}
}
else
{
if (date == 28)
{
valid = true;
}
else {
System.out.println("Invalid date: " + year + " is not a leap year, February does not have " + date + " days, please enter a valid date.");
}
}
break;
}
if(valid == true) {
System.out.println(dayNumber + " " + monthNumber + " " + date + " " + year + " is " + dayName + " " + monthName + " " + date+ ", " + year);
}
}
}
Fix output for
INPUT: 8 9 21 2001 OUTPUT: Invalid day number: 8, please enter a number from 1..7. 8 9 21 2001 is September 21, 2001
EXCEPTED OUTPUT: Invalid day number: 8, please enter a number from 1..7.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started