2016-09-28 55 views
0

我被要求制作一个程序,它会接收用户输入的日期并检查几个标准:日期长度,闰年,月份准确性,日期准确性等。 。我相信我已经得到了一切想通了,除了当我测试的日期“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."); 
           } 
        } 
       } 
      }     


     }    
    } 
} 
+1

使用java日期类来解析date.you不需要你自己做这一切。 –

+1

我不明白你为什么要这么做。如果DateFormatter失败,那么这是一个无效日期 - 故事结束。 –

+0

你不能用if语句捕捉所有可能的输入错误。这就是存在异常捕获的原因。另外,详细的消息很好,但是过分。 “无效的日期”可能是足够的错误消息。 –

回答

1

在你的代码类的问题就在这里: -

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 
} 

在这里,你正在寻找两个条件是真实的,而不是你应该使用来代替

这个你需要做一个小的变化: -

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 
} 

我会建议你使用由JAVA

定的Date类谢谢你, 快乐帮:)

1

你或许应该使用类似的点DateTimeFormatter.parse(CharSequence)情况下,任何的帮助是你的规则eap年是不正确的。如果年份可以被100整除,那么它不是闰年,尽管年份可以被4除尽,除非它也可以被400整除。在这种情况下,它是一个闰年。

日期是很难,你头到时区的土地,甚至之前...

+0

在这个问题上没有时间 –

+0

@ScaryWombat你是对的,如果你没有对日期做任何其他事情,直接使用DateTimeFormatter会更好...更改我的答案以反映这一点。 –

0

如何使用已经存在的

System.out.println("Please enter a date in the format (mm/dd/yyyy): "); //prints to screen 
String dateString = keyboard.nextLine();//user enters dateString value 

SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yyyy"); 

try { 
    sdf.parse (dateString); 
} catch (ParseException ex) { 
    System.out.println("Invalid date.");//prints to screen 
}