ParseException Java [英] ParseException Java

查看:110
本文介绍了ParseException Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing an appointment program that allows the user to input appointment dates, description, and type of appointment. Everything works correctly until they make a selection to "Print Range" which prints a range of dates, when they choose to do this it tells them to enter START date and an END date, the program then pulls all of the appointments from between those dates and displays them into the output box.

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

Here are the errors I am getting with print range :

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块,但是我不确定该怎么做,并且想知道是否有人可以为我提供解决这些错误的答案或示例.

I think I should probably do a try/catch block but am not real sure of how to do that, and was wondering if someone could provide me an answer or example to fix these errors.

这是我认为其中发生解析错误的一些代码:

Here is some of my code where I believe the parse error is taking place :

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

           }
        }
     }

推荐答案

Parse Exception是已检查的异常,因此必须处理它. 可以通过抛出或尝试捕获块来实现.

Parse Exception is checked exception so you must have to handle it. Either by throws or try catch block.

public static void main (String[] args)

应该是

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

或者在try catch块中

Or in 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();
  }

这篇关于ParseException Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆