ParseException Java

时间:2013-04-20 04:44:55

标签: java parseexception

我正在编写一个约会计划,允许用户输入约会日期,描述和约会类型。一切正常,直到他们选择“打印范围”打印一系列日期,当他们选择这样做它告诉他们输入开始日期和结束日期时,程序然后从这些日期之间拉出所有约会并将它们显示在输出框中。

以下是我在打印范围内遇到的错误:

AppointmentNew.java:68: unreported exception java.text.ParseException; must be caught or declared to be thrown
        Date lowDate = sdf.parse(stdin.nextLine());
                                ^
AppointmentNew.java:70: unreported exception java.text.ParseException; must be caught or declared to be thrown
        Date highDate = sdf.parse(stdin.nextLine());  
                                 ^
AppointmentNew.java:77: unreported exception java.text.ParseException; must be caught or declared to be thrown
           Date newCurrentDate = sdf.parse(currentDate); 

我想我应该尝试一下try / catch块,但我不确定如何做到这一点,并且想知道是否有人可以给我一个答案或示例来解决这些错误。

以下是我的一些代码,我认为发生了解析错误:

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AppointmentNew 
{
public static void main (String[] args) throws Exception
{

if (choiceNum == 2)
     {
        System.out.print("\n\n\tEnter START Date in mm/dd/yyyy format: ");
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        Date lowDate = sdf.parse(stdin.nextLine());
        System.out.print("\n\n\tEnter END Date in mm/dd/yyyy format: ");
       Date highDate = sdf.parse(stdin.nextLine());  

        for(int i = 0; i < list.size(); i++)
        {
           int dateSpot = list.get(i).indexOf(" ");
           String currentDate = list.get(i);
           currentDate.substring(0, dateSpot);
           Date newCurrentDate = sdf.parse(currentDate); 

           if (newCurrentDate.compareTo(lowDate) >= 0 && newCurrentDate.compareTo(highDate) <= 0)
           {
              System.out.println("\n\t" + list.get(i));

           }
        }
     }

2 个答案:

答案 0 :(得分:16)

为什么会收到错误:

enter image description here

如何修复它:

try catch围绕有问题的代码 - 就像说,我将捕获错误,记录它并希望能够对此做些什么。

 try {  // add this line
             System.out.print("\n\n\tEnter START Date in mm/dd/yyyy format: ");
             SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
             Date lowDate = sdf.parse(stdin.nextLine());
             System.out.print("\n\n\tEnter END Date in mm/dd/yyyy format: ");  
             Date highDate = sdf.parse(stdin.nextLine());  

             for(int i = 0; i < list.size(); i++)
             {
                int dateSpot = list.get(i).indexOf(" ");
                String currentDate = list.get(i);
                currentDate.substring(0, dateSpot);
                Date newCurrentDate = sdf.parse(currentDate); 

                if (newCurrentDate.compareTo(lowDate) >= 0 && newCurrentDate.compareTo(highDate) <= 0)
                {
                   System.out.println("\n\t" + list.get(i));   
                }
             }
         } catch (ParseException ex) {
              ex.printStackTrace(); // or log it using a logging framework
         }

或者把它扔到主处 - 这里,就像说:无论是谁在调用这种方法,如果它发生了,请处理这个问题

public static void main (String[] args) throws Exception

答案 1 :(得分:10)

Parse Exception被检查异常,因此您必须处理它。 通过抛出或尝试捕获阻止。

public static void main (String[] args)

应该是

public static void main (String[] args) throws ParseException

或者在try catch block中

try {
    //All your parse Operations
} catch (ParseException e) {
   //Handle exception here, most of the time you will just log it.
   e.printStackTrace();
  }
相关问题