Looping and exception handling in java main method

I working on a fairly basic problem: I am writing a java program that takes in strings for a day, month, and year (in integer form e.g. 24/9/2015) and then coverts this into a word based version of the date (e.g. 24th of September, 2015).

I also need to be able to handle a user from typing in “invalid” values for the above by creating my own exception classes.

When an exception is thrown (for example the user inputs an “invalid” month), the program should prompt the user to re-enter only the incorrect value (so in this case it prompts them to re-enter just the month).

Here is the code I have written so far (you may assume the exception classes have been properly defined and work as they should):

import java.util.Scanner;

public class ParseDate {

public static void main(String args[]){

    String month, day, year;
    Scanner input = new Scanner(System.in);

    System.out.println("Enter the day: ");
    day = input.nextLine();
    System.out.println("Enter the month: ");
    month = input.nextLine();
    System.out.println("Enter the year: ");
    year = input.nextLine();

    try{
        formatDate(month, day, year);
    }
    catch(MonthException m){
        //?
    }
    catch(DayException d){
        //??
    }
    catch(YearException y){
        //???
    }
}

public static String formatDate(String month, String day, String year)
throws MonthException, DayException, YearException{
    String output;
    if (Integer.parseInt(month) < 1 || Integer.parseInt(month) > 12){
        throw new MonthException("Invalid month. reenter.");
    }
    if (Integer.parseInt(year) < 1000 || Integer.parseInt(year) > 3000){
        throw new MonthException("Invalid year. reenter.");
    }
    if (Integer.parseInt(day) < 1){
        throw new MonthException("Invalid day. reenter.");
        //extra code to handle the cases where the day is 
        //too large for the particular month and year entered
    }

    //other code here for converting date

    return output;
}
}

As you can probably see from my code, I am having trouble deciding where and how I should get the program to loop through the exceptions separately. I was thinking of possibly using a switch but I’m not too sure where to put it.

How can I do this?

Answer

So, basically, you need someway to maintain the information between loops, each time an exception is triggered, you need to invalidate only the information related to the exception.

You then need to continue looping until all the values are validated and the values can be formatted, for example…

import java.util.Scanner;

public class ParseDate {

    public static void main(String args[]) {

        String day = null;
        String month = null;
        String year = null;
        String format = null;
        Scanner input = new Scanner(System.in);

        do {
            if (day == null) {
                System.out.println("Enter the day: ");
                day = input.nextLine();
            }
            if (month == null) {
                System.out.println("Enter the month: ");
                month = input.nextLine();
            }
            if (year == null) {
                System.out.println("Enter the year: ");
                year = input.nextLine();
            }

            try {
                format = formatDate(month, day, year);
            } catch (MonthException m) {
                System.err.println(month + " is not a valid month");
                month = null;
            } catch (DayException d) {
                System.err.println(day + " is not a valid day");
                day = null;
            } catch (YearException y) {
                System.err.println(year + " is not a valid year");
                year = null;
            }
        } while (format == null);
    }

    public static String formatDate(String month, String day, String year)
                    throws MonthException, DayException, YearException {
        String output = "Happy";

        int monthNum = 0;
        int dayNum = 0;
        int yearNum = 0;

        try {
            monthNum = Integer.parseInt(month);
            if (monthNum < 1 || monthNum > 12) {
                throw new MonthException("Invalid month. reenter.");
            }
        } catch (NumberFormatException exp) {
            throw new MonthException("Invalid month. reenter.");
        }
        try {
            yearNum = Integer.parseInt(year);
            if (yearNum < 1000 || yearNum > 3000) {
                throw new YearException("Invalid year. reenter.");
            }
        } catch (NumberFormatException exp) {
            throw new YearException("Invalid year. reenter.");
        }
        try {
            dayNum = Integer.parseInt(day);
            // You need to take into consideration the actual month,
            // but I'll leave that to you...
            if (dayNum < 1 || dayNum > 31) {
                throw new DayException("Invalid day. reenter.");
            }
        } catch (NumberFormatException exp) {
            throw new DayException("Invalid day. reenter.");
        }

        //other code here for converting date
        return output;
    }

    public static class MonthException extends Exception {

        public MonthException(String message) {
            super(message);
        }

    }

    public static class DayException extends Exception {

        public DayException(String message) {
            super(message);
        }

    }

    public static class YearException extends Exception {

        public YearException(String message) {
            super(message);
        }

    }
}

Now, frankly it would be simpler to validate each one individually, but that’s me

Leave a Reply

Your email address will not be published. Required fields are marked *