Question
I need help fixing the errors in my code. import java.util.Scanner; class MonthException extends Exception{ public MonthException(){ super(Invalid month.); } } class DayException extends Exception{
I need help fixing the errors in my code.
import java.util.Scanner;
class MonthException extends Exception{
public MonthException(){
super("Invalid month.");
}
}
class DayException extends Exception{
public DayException(){
super("Invalid day.");
}
}
class YearException extends Exception{
public YearException(){
super("Invalid year.");
}
}
public class TestException{
public static void main(String[] args) {
int monthnum;
int monthDays;
String monthName="";
String date="";
Scanner input=new Scanner(System.in);
System.out.println("Please enter a date in this format: Month/Day/Year.");
date=input.next();
String[] pars=date.split("/");
int month=Integer.parseInt(pars[0]);
int day=Integer.parseInt(pars[1]);
int year=Integer.parseInt(pars[2]);
switch(monthnum){
case 1:
monthName="January";
monthDays=31;
case 2:
monthName="February";
monthDays=28;
case 3:
monthName="March";
monthDays=31;
case 4:
monthName="April";
monthDays=30;
case 5:
monthName="May";
monthDays=31;
case 6:
monthName="June";
monthDays=30;
case 7:
monthName="July";
monthDays=31;
case 8:
monthName="August";
monthDays=31;
case 9:
monthName="September";
monthDays=30;
case 10:
monthName="October";
monthDays=31;
case 11:
monthName="November";
monthDays=30;
case 12:
monthName="December";
monthDays=31;
default:
System.out.println("Not valid.");
}
while(true){
try{
if(month<1||month>12){
throw new MonthException("The month must be numbers 1-12.");
}
else{
break;
}
}
catch(MonthException e){
System.out.println("Please enter a valid month: ");
month=input.next();
continue;
}
}
while(true){
try{
if(day<1||day>monthDays){
throw new DayException("That day does not exist in this month.");
}
else{
break;
}
}
catch(DayException e){
System.out.println("Please enter a valid day: ");
day=input.next();
continue;
}
}
while(true){
try{
if(year<=1000||year>=3000){
throw new YearException("The year must be between 1000 and 3000.");
}
else{
break;
}
}
catch(YearException e){
System.out.println("Please enter a valid year: ");
year=input.next();
continue;
}
}
System.out.println("The date conversion is: "+monthName, +day, ", " +year);
}
}
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