我被要求制作一个程序,它会接收用户输入的日期并检查几个标准:日期长度,闰年,月份准确性,日期准确性等。 。我相信我已经得到了一切想通了,除了当我测试的日期“12/345/678”我得到了一系列的Java错误包括:验证日期输入 - 修复java.lang.numberformatexception错误
Exception in thread "main" java.lang.NumberFormatException: For input string: "/678"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DateChecker.main(DateChecker.java:42)
我认为,这是因为当java的尝试提取“yearString”时,它无法使用,因为它已经使用intparser转换为整数,并且输入的代码现在包含“/”,所以与日期“1/234/5678”会发生相同的错误,因为monthString正在查找所有整数,但它现在包含一个“/”。
我知道下面的代码,因为我已经通过另一个类似的问题阅读了另一个StackOverflow文章,虽然当我试图将它应用到我的程序时,我得到了一系列的编译器错误,因为我的变量现在被关闭了程序
try{
int i = Integer.parseInt(input);
}catch(NumberFormatException ex){ // handle your exception
...
}
这里休息是我到目前为止的代码,并在此先感谢所提供:)
import java.util.*;
public class DateChecker {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); //activates the keyboard
String dateString; //variable declaration
System.out.println("Please enter a date in the format (mm/dd/yyyy): "); //prints to screen
dateString = keyboard.nextLine();//user enters dateString value
if (dateString.length()>10)//checks if date is greater than 10
{
System.out.println("Invalid date."); //prints to screen
System.out.println("To many characters in the date.");//prints to screen
}
else if(dateString.length()<10)//checks if date is less than 10
{
System.out.println("Invalid date.");//prints to screen
System.out.println("To few characters in the date.");//prints to screen
}
else {//date = 10
if(dateString.charAt(2)!='/' && dateString.charAt(5)!='/')//checks for "/" at spots 2 and 5
{
System.out.println("Invalid date.");//prints to screen
System.out.println("Incorrect format.");//prints to screen
}
else{//"/" at spots 2 and 5
//declares variables and extracts strings
String yearString = dateString.substring(6, 10);
String dayString = dateString.substring(3, 5);
String monthString = dateString.substring(0, 2);
//converts string variables to integer
int monthInt=Integer.parseInt(monthString);
int dayInt=Integer.parseInt(dayString);
int yearInt=Integer.parseInt(yearString);
if(monthInt < 1|| monthInt > 12)//checks if valid month is entered
{
System.out.println("Invalid date.");//prints to screen
System.out.println("Month is not valid.");//prints to screen
}
else
{//month is valid
if(dayInt < 1)
{
System.out.println("Invalid date.");
System.out.println("Day is not valid.");
}
else {
if((monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11) && dayInt > 30)//checks if months should have 30 days
{
System.out.println("Invalid date.");//prints to screen
System.out.println("Day is not valid.");//prints to screen
}
else
if (yearInt % 4 ==0 && (monthInt == 2 && dayInt > 29))//checks if leap year
{
System.out.println("Invalid date.");//prints to screen
System.out.println("Day is not valid.");//prints to screen
}
else//if not leap year
if (yearInt % 4 != 0 && dayInt > 28)//checks if normal year
{
System.out.println("Invalid date.");//prints to screen
System.out.println("Day is not valid.");//prints to screen
}
else //date entered was valid
{
System.out.println("Valid date.");
}
}
}
}
}
}
}
使用java日期类来解析date.you不需要你自己做这一切。 –
我不明白你为什么要这么做。如果DateFormatter失败,那么这是一个无效日期 - 故事结束。 –
你不能用if语句捕捉所有可能的输入错误。这就是存在异常捕获的原因。另外,详细的消息很好,但是过分。 “无效的日期”可能是足够的错误消息。 –