Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I cannot figure out how to fix my program. It is supposed to convert a numerical date to the formal format (i.e 11/5/2018 -> November

I cannot figure out how to fix my program. It is supposed to convert a numerical date to the formal format (i.e 11/5/2018 -> November 5, 2018). It always says the month is December and it sometimes throws an exception when it's not supposed to (i.e saying 13 is not a valid month day).

import java.util.Scanner;

class MonthException extends Exception{

public MonthException(String message){

super(message);

}

}

class DayException extends Exception{

public DayException(String message){

super(message);

}

}

class YearException extends Exception{

public YearException(String message){

super(message);

}

}

public class TestException{

public static void main(String[] args) {

int monthnum;

int monthDays=0;

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(month){

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.nextInt();

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.nextInt();

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.nextInt();

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

Students also viewed these Databases questions

Question

please show all steps. Thank you in

Answered: 1 week ago