【问题标题】:ParseException Java解析异常 Java
【发布时间】:2013-04-13 13:22:13
【问题描述】:

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

这是我在打印范围时遇到的错误:

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));

           }
        }
     }

【问题讨论】:

    标签: java parseexception


    【解决方案1】:

    Parse Exception 是检查异常,所以你必须处理它。 通过 throws 或 try catch 块。

    public static void main (String[] args)
    

    应该是

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

    或者在try catch块中

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

    【讨论】:

    • @jmsutton334 我在回答中做了一些修改。如果它按你想要的方式工作,你可以接受我的回答。
    【解决方案2】:

    为什么会出现错误:

    如何解决:

    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
             }
    

    或者把它扔到 main - 在这里,就像在说:不管是谁在调用这个方法,如果它发生,请处理这个问题

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

    【讨论】:

    • Here's an answer首先提供了有关为什么会发生此错误的一些解释。 :)
    • 那张图真的很酷,你是用什么程序做的?
    • 虽然其他答案直截了当,但您的答案读起来很有趣。无聊的老师和有趣的老师的区别。赞一个!
    猜你喜欢
    • 2016-12-26
    • 1970-01-01
    • 2019-05-23
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多