如何在下面的程序中包含错误消息?

时间:2014-03-24 04:32:47

标签: java

我是一名初学IT11学生,我们应该制作一个程序,该程序将读取一个人出生的日期,月份和年份的数量。

例如,1982年9月3日出生的人将输入数字

3,9和1982

分为三个单独的JOP.showInputDialogs

如果输入的值不正确,程序应该给出一个非常具体的错误消息,说明它无效的原因,并要求用户再次输入信息。在进行无效输入时,用户永远不必重新输入多个值(例如,如果日期无效,则用户只需重新输入日期,而不是月份或年份。)

该程序将以下列格式告诉该人他们的出生日期:

你出生于1982年9月3日。

日期格式必须如上所示。

重要 - 程序必须对无效月份进行错误检查(有效期在1到12之间) - 程序必须对无效年份进行错误检查(有效> = 1800) - 程序必须对月份无效的日期进行错误检查(有效期为1到月之间(30,31,28或29)) - 程序必须仅在LEAP年份允许2月29日。

我坚持使用的部分包含无效日期的错误消息。例如,如果我在4月31日输入,该程序应该返回一条错误消息,说明" 4月只有30天"等等。我该怎么做?这是我到目前为止所得到的。


import javax.swing.*;
public class A6DateProgram {

 public static void main(String[] args) {
    int year = getYearFromUser();  //gets user input for year
    int month = getMonthFromUser();  //gets user input for month
    int day = getDateFromUser(month, year);  //gets user input for date


    //tells the user their birthdate
    System.out.println("You were born " + Months(month) + " " + day + ", "+ year + " " + ".");
}  //main

//asks user for year 
public static int getYearFromUser(){
    String year; //asks user for year
    int year1 = 0;
    String errorMessage = "";
    boolean isLeap = false;

    do{
        year = JOptionPane.showInputDialog(errorMessage +  "Please enter the year you were born in. (>1800)");

        if (year == null) {
        System.out.println("You clicked cancel");
        System.exit(0);
        } 

        // parse string to an int
        try {
            year1 = Integer.parseInt(year);  //parses recieved number to an int
        } catch (Exception e) {
            errorMessage = "Invalid integer\n";  //if user does not input valid integer
            continue;  //continues to condition [while(true);]
        } // catch

        isLeap = validateYear(year1);

        if(year1 < 1800 || year1 > 2400){  //number limitation
            errorMessage = "Your number must be greater than 1800 or less than 2400. \n"; //if user does not input a valid integer between limit
            continue;  //continues to condition [while(true);]
        }
            break;
        } while(true);
          return year1;
  } //getYearFromUser

public static boolean validateYear(int year){
return (year % 400 == 0 ) ? true : (year%100 == 0)? false : (year % 4 == 0)? true: false;
}
//asks user for month
public static int getMonthFromUser(){
    String month;
    int num = 0;
    String errorMessage = "";

    do{
        month = JOptionPane.showInputDialog(errorMessage +  "Please enter the month you were born in as a valid integer. (ex. January = 1)");

        if (month == null) {
        System.out.println("You clicked cancel");
        System.exit(0);
        }

        // parse string to an int
        try {
            num = Integer.parseInt(month);
        } catch (Exception e) {
            errorMessage = "Invalid integer\n";
            continue;  //continues to condition [while(true);]
        } // catch

        if(num > 12 || num < 1){
            errorMessage = "A year only has 12 months. \n";
            continue;  //continues to condition [while(true);]
        }
            break;
        } while(true);
          return num;
        } //getMonthFromUser


//asks user for date
public static int getDateFromUser(int month, int year){
    String date;
    int day = 0;
    String errorMessage = "";
    boolean ToF = false;

    do{
        date = JOptionPane.showInputDialog(errorMessage +  "Please enter the date you were born in. (1-31)");

        //user clicks cancel
        if (date == null) {
        System.out.println("You clicked cancel");
        System.exit(0);
        }
        // parse string to an int
        try {
            day = Integer.parseInt(date);
        } catch (Exception e) {
            errorMessage = "Invalid integer\n";
            continue;  //continues to condition [while(true);]
        } // catch

         ToF = validate(year, month, day); //giving boolean ToF a method to validate the day

         if(ToF == false){
            errorMessage = "The month you input does not have that date. \n";
            continue;  //continues to condition [while(true);]
        }
        break;

    } while(true);  //do
     return day;
}    //getDateFromUser

public static boolean validate(int year, int month, int day){
switch(month){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        if(day < 1 || day > 31)
            return false;
        break;
    case 2:
        if(year%4 == 0 || (year%400 == 0 && year%100 != 0)){
            if (day < 1 || day > 29)
                return false;
        }else{
            if (day < 1 || day > 28)
                return false;
        }
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        if(day < 1 || day > 30)
            return false;
        break;
}

return true;
}   //validate

//resonse to user input for month
public static String Months(int month) {
switch (month) {
case 1: return "January";
case 2: return "Febrary";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";

    default: return "Invalid";
    } // switch
} //Months
} //A6DateProgram Class

1 个答案:

答案 0 :(得分:1)

尝试使用SimpleDateFormat()

String date  = "3";
String month = "9";
String year  = "1980";
SimpleDateFormat sdf1 = new SimpleDateFormat("ddMMyyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMMM dd, yyyy");
Date date1 = sdf1.parse((Integer.parseInt(date)<10?"0"+date:date)+(Integer.parseInt(month)<10?"0"+month:month)+year);
String ansStr = sdf2.format(date1);
System.out.println("You were born "+ansStr);

如果您输入一个生效日期,它会自动进入下一个日期。

如果输入为29-02-2014,则为Exmple,将为01-03-2014