簡體   English   中英

Java main方法中的循環和異常處理

[英]Looping and exception handling in java main method

我正在研究一個相當基本的問題:我正在編寫一個Java程序,該程序接受一天,月份和年份的字符串(以整數形式,例如24/9/2015),然后將其轉換為日期的基於單詞的版本(例如2015年9月24日)。

我還需要能夠通過創建我自己的異常類來處理用戶從上面鍵入“無效”值的問題。

當引發異常時(例如,用戶輸入“無效”月份),程序應提示用戶重新輸入錯誤的值(因此,在這種情況下,它將提示用戶重新輸入月份)。

這是我到目前為止編寫的代碼(您可以假設異常類已經正確定義並且可以正常工作):

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;
}
}

正如您可能從我的代碼中看到的那樣,我在確定應該在哪里以及如何使程序分別循環遍歷異常時遇到麻煩。 我本來想使用switch但不太確定該在哪里放置switch

我怎樣才能做到這一點?

因此,基本上,您需要某種方式來維護循環之間的信息,每次觸發異常時,您只需要使與異常相關的信息無效即可。

然后,您需要繼續循環直到所有值都經過驗證並且可以格式化值為止,例如...

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);
        }

    }
}

坦率地說,現在單獨驗證每個對象會更簡單,但這就是我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM