Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions

Question

What utility is used to search for files or directories?

Answered: 1 week ago