java - 如何在下面的程序中加入错误消息?

标签 java

我是一名 IT11 初学者,我们应该编写一个程序来读取一个人出生的年、月、日的数字。

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

3、9 和 1982 年

分成三个独立的JOP.showInputDialogs

如果输入了错误的值,程序应给出非常具体的错误消息,解释其无效原因,并要求用户再次输入信息。当进行无效输入时,用户不应重新输入多个值(例如,如果日期无效,则用户只需重新输入日期,而不是月份或年份)。

程序将按照以下格式告诉该人他们的生日:

您出生于 1982 年 9 月 3 日。

日期的格式必须如上所示。

重要 - 程序必须对无效月份进行错误检查(1 到 12 之间有效) - 程序必须对无效年份进行错误检查(有效> = 1800) - 程序必须对月份中的无效日期进行错误检查(在月份中的 1 到 maxDay 之间有效(30、31、28 或 29)) - 该计划必须只允许闰年的 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

最佳答案

尝试使用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,则将视为01-03-2014

关于java - 如何在下面的程序中加入错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22601145/

相关文章:

java - 继续使用 PHP 或迁移到 Java 框架

java - 为什么内存管理在 Java VM 中如此可见?

java - 连接Java和Teradata : The UserId, 密码或账号无效

java - 在 Eclipse 中使用 Java 创建 REST 服务

c# - 单声道不能消费触摸事件

java - 结合 BigQuery 和 Pub/Sub Apache Beam

java - 中断阻塞网络IO而不关闭socket

java - jsp 中从 struts1 到 struts2 的一些语法更改 --> 逻辑 :messagesPresent

使用 parallelStream 和 forEach 的 Java 并行化,其中每个部分都是完全独立的

java - 通过类加载器以丑陋的方式忽略 JARS